ngOnChanges仅执行一次

时间:2019-11-15 05:50:37

标签: angular typescript angular8

我在子组件中使用了ngonchange,如下所示。但是我看到它只执行一次。所以基本上我在子组件中有一个下拉列表,所以当我在下拉列表中更改某些内容时,我希望执行ngonchange,这仅发生一次。我第二次更改项目,则ngonchange不会执行。

ngOnChanges(changes: SimpleChanges){
  console.info(changes.test);
  if(this.test!=null){
    console.info(this.test);
    this.currentSnapshotPeriod = this.snapshotPeriods.filter(x => x.periodName === this.test)[0];
 this.onCurrentSnapshotPeriodChange();
 changes.SimpleChanges.currentValue = undefined;
  }
}

父HTML:

<app-snapshot-period-dropdown
                       [test]=input
                         (change)="abc($event)"
                        >
                    </app-snapshot-period-dropdown>

子HTML:

<select
      id="currentPeriodDropDown"
      (change)="onCurrentSnapshotPeriodChange()"
      [(ngModel)]="currentSnapshotPeriod"
      class="form-control form-control-sm mr-1 form-control-sm-ipad"
    >
      <option [ngValue]="period" *ngFor="let period of snapshotPeriods"
        >{{ period?.periodName }}
      </option>
    </select>

1 个答案:

答案 0 :(得分:0)

ngOnChanges仅在从父组件更改@Input()时触发,而不是在子组件中更改时触发。这是我在做的错误