[英]Angular : ngOnChanges is not detecting changes?
I've a dropdown list in one component . 我在一个组件中有一个下拉列表 。 Each time the value of dropdown changes I'm trying to detect the changed value in another component .
每次下拉列表的值更改时,我都会尝试检测另一个组件中 更改的值 。 But I've a strange problem.
但是我有一个奇怪的问题。 Sometimes, when I change the value of dropdown it's triggering the ngOnChanges and sometimes not.
有时,当我更改dropdown的值时,它会触发ngOnChanges,有时却不会。 Idk why.
伊德为什么。
This is my code: 这是我的代码:
AppComp AppComp
html html
<td width="20%" class="cursor">
<select (change)="changedvalue($event)" class="form-control" name="level">
<option hidden selected> -- Select options -- </option>
<option>Level 1</option>
<option>Level 2</option>
</select>
</td>
<td class="cursor">
<app-other-comp [group]="group"></app-other-comp>
</td>
ts ts
group: string;
changedvalue(event: Event) {
const value = (<HTMLSelectElement>event.target).value;
this.group = value;
console.log('From AppComp' + this.group);
}
OtherComp 其他比较
ts ts
@Input() group: string;
ngOnChanges() {
console.log('Changed : ' + this.group);
}
As in the browser console you can see sometimes it detects a change and sometimes it does not. 就像在浏览器控制台中一样,您有时会看到它检测到更改,有时却没有检测到。
change doesn't get detected: 未检测到更改:
change gets detected: 检测到更改:
change function is called when input blurred. 当输入模糊时,将调用change函数。 you can use ngModel and ngModelChange function like this:
您可以像这样使用ngModel和ngModelChange函数:
<td width="20%" class="cursor">
<select (ngModelChange)="changedvalue($event)" [(ngModel)]="value" class="form-control" name="level">
<option hidden selected> -- Select options -- </option>
<option>Level 1</option>
<option>Level 2</option>
</select>
</td>
<td class="cursor">
<app-other-comp [group]="group"></app-other-comp>
</td>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.