当值从 select 更改时不会触发 ngOnChanges
ngOnChanges is not triggered when value changes from select
我有下一个html:
<select [(ngModel)]="district" class="form-control" (change)="dsChange()">
<option disabled hidden [value]="undefined" >Ноҳия</option>
<option *ngFor="let dis of districts" [ngValue]="dis">{{dis.title}}</option>
</select>
我还有下一个组件
@Input() district: District;
ngOnChanges(changes: SimpleChanges) {
console.log("CHANGED")
}
dsChange(){
console.log(this.district);
}
而且我不明白为什么当我 select 一些值 dsChange 被正确的区值触发,但是 ngOnChanges() 不是
ngOnChanges
在组件的数据绑定输入属性更改时调用。在您的情况下,如果父组件为 @Input() district
.
设置新值,则会调用它
ngOnChanges()
Respond when Angular (re)sets data-bound input properties...Called before ngOnInit() and whenever one or more data-bound input properties change.
即使变量是 component.I 的本地变量,它也不会调用 onChanges
为此创建了一个 plukr:
我有下一个html:
<select [(ngModel)]="district" class="form-control" (change)="dsChange()">
<option disabled hidden [value]="undefined" >Ноҳия</option>
<option *ngFor="let dis of districts" [ngValue]="dis">{{dis.title}}</option>
</select>
我还有下一个组件
@Input() district: District;
ngOnChanges(changes: SimpleChanges) {
console.log("CHANGED")
}
dsChange(){
console.log(this.district);
}
而且我不明白为什么当我 select 一些值 dsChange 被正确的区值触发,但是 ngOnChanges() 不是
ngOnChanges
在组件的数据绑定输入属性更改时调用。在您的情况下,如果父组件为 @Input() district
.
ngOnChanges()
Respond when Angular (re)sets data-bound input properties...Called before ngOnInit() and whenever one or more data-bound input properties change.
即使变量是 component.I 的本地变量,它也不会调用 onChanges
为此创建了一个 plukr: