【发布时间】:2016-04-06 10:34:57
【问题描述】:
这里是显示数据组件:
@Component({
selector: 'show-data',
template: `yes! Now showing the show-data directive template !`
})
export class ShowData {}
及其父级:
@Component({
selector: 'my-app',
template: `
The 'shouldShow' boolean value is: {{shouldShow}}
<show-data *ngIf="shouldShow"></show-data>
<div *ngIf="!shouldShow">NOT showing the show-data directive template</div>
`,
directives: [ShowData]
})
export class App {
shouldShow:boolean = false;
constructor(){
console.log("shouldShow value before timeout",this.shouldShow);
window.setTimeout(function(){
this.shouldShow = true;
console.log("shouldShow value after timeout",this.shouldShow);
}, 1000);
}
}
最初,shouldShow 变量设置为 false,show-data 指令模板不显示。很好。
shouldShow 在一秒钟后被父组件构造函数设置为“true”。
为什么父组件视图中shouldShow的值没有更新?
这是plunkr
【问题讨论】: