【问题标题】:ngOnChanges gets executed only oncengOnChanges 只执行一次
【发布时间】:2019-11-15 14:18:57
【问题描述】:

我在我的子组件中使用了 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>

【问题讨论】:

  • 如果你分享stackblitz会更好
  • 你能把html部分贴出来看看你是如何使用绑定的
  • @JoelJoseph 我已经添加了 HTML
  • @JoelJoseph 实际上我所做的是我创建了 abc 函数并且该函数调用 onCurrentSnapshotPeriodChange($event)
  • @taashibhulani 是的,我看到了

标签: angular typescript angular8


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2019-11-15
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    相关资源
    最近更新 更多