我有一個名爲model的輸入變量,用於childComponent。 @Input()model:any;Angular 4:如何應對@input變量的變化
我有一個父控件parentControl將更新的輸入變量發送到childComponent。
如何編碼childComponent對模型更改做出反應? 我想要在模型更改時進行childComponent調用。
想法?
我有一個名爲model的輸入變量,用於childComponent。 @Input()model:any;Angular 4:如何應對@input變量的變化
我有一個父控件parentControl將更新的輸入變量發送到childComponent。
如何編碼childComponent對模型更改做出反應? 我想要在模型更改時進行childComponent調用。
想法?
考慮使用ngOnChanges()生命週期
@Input() model: any;
ngOnChanges(model: any) {
this.doSomething(model);
}
,如果有多個輸入參數,我會爲每個輸入變量有一個ngOnChanges嗎? – reza
是的,你需要這樣做,在angular2中沒有這樣的watchgroup,就像我們在angularjs中一樣 – Sajeetharan
@reza no,你不這樣做,單個'ngOnChanges'方法的參數實際上是'SimpleChanges',它包含*任何*的輸入。你可以在鏈接的文檔中看到這個,不知道作者爲什麼會這樣說。 – jonrsharpe
https://angular.io/api/core/OnChanges – Alex
這是勿庸置疑,在文檔:https://angular.io/guide/component -interaction – jonrsharpe
更準確地說是https://angular.io/guide/component-interaction#intercept-input-property-changes-with-a-setter – Vega