angular - ngOnChanges 只执行一次
问题描述
我在我的子组件中使用了 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>
解决方案
ngOnChanges 仅在 @Input() 从父组件更改时触发,而不是在您在子组件中更改它时触发。这是我犯的错误
推荐阅读
- json - 为什么 IATA 选择 XML 而不是 JSON 来提供 NDC 服务?
- jenkins - Jenkins 在同一个存储库中构建多个项目
- sql - 红移模式匹配
- c++ - 没有调用构造函数?
- javascript - 如何在 React Native 的类 Component 中将函数作为道具传递和执行?
- flutter - 如何从 Flutter 中使用 Firestore 中的唯一文档 ID 进行查询?
- oauth - 有没有办法为 botframework 中的 SingleSignOn (SSO) 生成令牌以访问同一租户下的多个资源
- c# - 如果 nuget 已作为 nuget 依赖项安装,但它在您的项目中不可见,如何安装它?
- angular - 如何在 Angular 中进行基于角色的路由?
- javascript - 无法正确渲染项目卡。Reactjs Gatsbyjs Graphql