2016-03-15 14 views
0

我在打字稿中構建了一個IONIC 2項目。我有以下的CSS:Typescript打擊scss風格屬性的麻煩

.bottle { 
    background: linear-gradient(0deg, pink 100%, white 0%); 
} 

,我想調整的線性梯度從pink值,從Javascript/Typescript.I已經做了我的打字稿以下,但我得到了一個錯誤說, styleFile元素不存在於BottleFill上。我做錯了什麼?

import {Page, Alert, NavController} from 'ionic-angular'; 

@Page({ 
    templateUrl: 'build/pages/hello-ionic/hello-ionic.html' 
}) 

export class HelloIonicPage { 
    constructor(public nav: NavController) { } 

    updateDisplay(batteryPercentage){ 
     var bottleFill = document.getElementsByClassName('bottle')[0]; 

     if(percentage <= 100){ 
      bottleFill.style.linerar-gradient.pink = batteryPercentage + '%'; 
      batteryPercentage += 5; 
     }   
    } 
} 

更新:

完整的錯誤:使用ngOnInit使用元素選擇沒有工作

The property 'style' does not exist on type 'Element'

...

+0

是bottleFill不確定?該代碼可能在元素呈現之前運行。 –

回答

0

也許你應該檢索在的CSS ngAfterViewInit方法,一旦視圖初始化,由Angular調用: http://learnangular2.com/lifecycle/

+0

恐怕沒有幫助,我只是得到「bottleFill無法識別」,我會用更多的細節更新問題 –

0

您可以使用類型斷言通知打字稿,它實際上是HTML元素:

var bottleFill = document.getElementsByClassName('bottle')[0] as HTMLElement;