我将父组件的不同id
传递给子组件。如何知道@Input何时更改?
// 父组件
<child-component id={{id}}></child-component>
// 子组件
@Input('id') id:string;
如何获得时id
变化的事件?我想在子组件中更改id
时运行一个函数。
谢谢
我将父组件的不同id
传递给子组件。如何知道@Input何时更改?
// 父组件
<child-component id={{id}}></child-component>
// 子组件
@Input('id') id:string;
如何获得时id
变化的事件?我想在子组件中更改id
时运行一个函数。
谢谢
实施ngOnChanges(changes)
。它在输入值改变时被调用。
这只是ngOnInit()
你也可以写一个setter之前调用一次。根据我的经验,当@Input()位于数组上时,不会调用setter或ngOnChanges,因为在此情况下数组的引用不会更改。幸运的是,如果数组项更改,则会调用ngDoCheck。
https://angular.io/docs/ts/latest/cookbook/component-communication.html
谢谢你,Günter! –
为他人:https://angular.io/docs/js/latest/api/core/OnChanges-interface.html –