我有一個簡單的組件,它只是呈現一個進度條。當變量發生變化時,Angular2組件視圖不會更新
它初始化很好,進度傳遞給它很好,但模板沒有用新值更新。
import {Component} from 'angular2/core';
@Component({
selector : 'progress-bar',
template : `
<div class="progress-container">
<div>{{currentProgress}}</div>
<div [style.width.%]="currentProgress" class="progress"></div>
</div>
`,
styles: [
'.progress-container {width: 100%; height: 5px}',
'.progress {background-color: #8BC34A; height:5px}'
]
})
export class ProgressBar {
currentProgress: number;
constructor() {
this.currentProgress = 0;
}
onProgress(progress: number) {
console.log(progress) //consoles correct percentages
this.currentProgress = progress;
}
reset() {
console.log(this.currentProgress) //is 100
this.currentProgress = 0;
}
}
~
別處
ngOnInit() {
this.httpFileService.progress$.subscribe((progress: number) => this.onProgress(progress));
}
onProgress(progress: number) {
this.progressBar.onProgress(progress*100);
}
我覺得我失去了一些東西非常的補救。
如果您將更新您的問題以向我展示'onProgress'是如何被調用的,我可能會讓我的答案對您更有幫助 - 我懷疑這裏有更深的反模式。 – drewmoore
添加了onProgress部件。它通過我訂閱的xhr onprogress檢索。 – Dreamlines
我是對的:-)只需一秒鐘,我就會更新 – drewmoore