Man*_*ube 3 angular2-template angular
这是show-data组件:
@Component({
selector: 'show-data',
template: `yes! Now showing the show-data directive template !`
})
export class ShowData {}
Run Code Online (Sandbox Code Playgroud)
而它的父母:
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
最初,该shouldShow变量设置为false,并且show-data不显示指令模板.精细.
shouldShow 然后由父组件构造函数在一秒后设置为"true".
为什么不在shouldShow父组件视图中更新值?
这是一个傻瓜
你的问题*ngIf本身并不存在.它是setTimeout(function(){...})因为this在匿名函数内部将引用函数本身而不是AppComponent实例.
因此,相反,能够访问该AppComponent实例.使用lambda表达式(也称为箭头函数).
这是你的plunker编辑
window.setTimeout(()=>{
this.shoulShow = true;
console.log("shoulShow value after timeout",this.shoulShow);
}, 1000);
Run Code Online (Sandbox Code Playgroud)
或者,您可以指定this一个新变量,以便能够从匿名函数内部访问它.
let that = this
window.setTimeout(function(){
that.shoulShow = true; // here use the new var 'that' instead of 'this'
console.log("shoulShow value after timeout",that.shoulShow);
}, 1000);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5803 次 |
| 最近记录: |