0

我有两个兄弟组件,如下所示

<broker-list (onBrokerClicked)="onBrokerClicked= $event"></broker-list>
<station-list [(broker)]="onBrokerClicked" [(ngModel)]="stations"></station-list><!--broker is an input-->

当用户单击代理列表时,在站列表组件代理中@Input被填充。in stationlist只有在ngModel单击其兄弟组件后才能填充,或者换句话说,输入代理被填充。我想要输入从不在的同级组件中填充的确切时刻。输入人口后是否有任何事件或某些事情我可以在其中做一些事情???ngOnInit

4

2 回答 2

1

您可以ngOnChanges通过从 导入onChanges来使用@angular/core

ngOnChanges 做什么?ngOnChanges 会在输入值更改时触发。

import { onChanges } from @angular/core;

在类中定义您的 ngOnChanges :

export your_class_name implements onChanges{

 ngOnChanges(){
 //Do your stuff with the input
 }
}
于 2017-06-07T06:27:27.690 回答
1

将setter方法添加到Input()属性

站列表.component.ts

private _broker;

@Input() set broker(value) {
     this._broker = value
     // do the stuff every time when broker value is changed
}

get broker(value) {
     return this._broker;
}
于 2017-06-06T12:29:17.113 回答