@Input in directive angular 6 不是双向绑定
@Input in directive angular 6 not two way binding
我的自定义指令 showError
,有一个输入:
@Input() isActive: boolean;
ngOnInit() {
console.log(this.isActive); // true
}
在 app.component.html
中,我使用此指令(默认为 isError = true
):
<div appShowError [isActive]="isError"></div>
然后调用服务并在回调中将 isError
设置为 false。
但是指令中的 isActive
不会将值更改为 false
。
您需要检查 Angular
的 ngOnChanges
钩子中的变化
ngOnChanges(changes: SimpleChanges) {
// Here you can check changes in the `isError`
}
以上是 @Input
情况下变化检测如何工作的分叉工作示例
我的自定义指令 showError
,有一个输入:
@Input() isActive: boolean;
ngOnInit() {
console.log(this.isActive); // true
}
在 app.component.html
中,我使用此指令(默认为 isError = true
):
<div appShowError [isActive]="isError"></div>
然后调用服务并在回调中将 isError
设置为 false。
但是指令中的 isActive
不会将值更改为 false
。
您需要检查 Angular
的ngOnChanges
钩子中的变化
ngOnChanges(changes: SimpleChanges) {
// Here you can check changes in the `isError`
}
以上是 @Input