我有一個側面導航欄,其中每個導航欄中都有一個進度欄。 這是模塊化結構 -動態更改css而無需在Angular2中重新加載頁面
MainFile
-Children1
-children1.html
-children1.component.ts
-Children2
-children2.html
-children2.component.ts
-Children3
-children3.html
-children3.component.ts
-ProgressIndicator
-progressIndicator.html
-progressIndicator.component.ts
-main.html
-main.component.ts
裏面progressIndicator.html,我有這樣的代碼,
<div class="progress">
<div [ngStyle]="{'width':getProgress()}" class="progress-bar" role="progressbar" aria-
valuenow="70" aria-valuemin="0" aria-valuemax="100">
<span class="sr-only">70% Complete</span>
</div>
而我progressIndicator.component.ts
currentPage = '';
progressBar = '';
//getting current route name using 'this.route.url' and updating 'currentPage' property in
constructor.
getProgress(){
if(currentPage === 'children1'){
return progressBar = '25%';
}
if(currentPage === 'children2'){
return progressBar = '50%';
}
}
我成功地獲得當前頁面,作爲'children1.html'或'children2.html',基於當前頁面我想改變div的寬度與類'progress-bar',我用
[ngStyle]="{width:getProgress()}"
但只有當頁面重新加載時,纔會生效,其中angular2是SPA不會發生。 幫助我找到一種更好的方式,當我從'children1'移動到'children2'時,'getProgress()'生效並更改樣式。
它仍然只在重新加載。 :( –