1
我在3秒後更新組件的消息,但它不在模板中更新。我試着安裝angular2-polyfills(有些人說是嘗試),我試過Angular2模板不更新
constructor(private cd: ChangeDetectorRef)
{ ....
this.cd.markForCheck();
....
}
,但似乎沒有任何工作。下面是完整的組件:
import { Component, ViewEncapsulation, ChangeDetectorRef } from
'@angular/core';
//Defines a standard component
@Component({
selector: 'app',
templateUrl: './App.component.html'
})
export class AppComponent {
message: String = "hello"; //Message im trying to update
//Just to try ChangeDetectorRef...
constructor(private cd: ChangeDetectorRef)
{
//Timer that sets the message to '"HELLOWITHOUTWORLD" after 3 seconds
window.setTimeout(function() {
this.message = "HELLOWITHOUTWORLD";
console.log("changed it " + this.message);
}, 3000);
//I ALSO TRIED DOING "this.zone.run" but kept getting zone is undefined
//window.setTimeout(function() { this.zone.run(() => { this.message = "HELLOWITHOUTWORLD"; console.log("changed it"); }); }, 3000);
//Some sites said that this would update the template...
this.cd.markForCheck();
}
}
和HTML:
{{message}}