javascript - Angular : ngOnChanges is not detecting changes?

标签 javascript angular typescript

我在一个组件中有一个下拉列表。每次下拉列表的值发生变化时,我都会尝试检测另一个组件中的更改的值。但我有一个奇怪的问题。有时,当我更改下拉列表的值时,它会触发 ngOnChanges,有时则不会。我不知道为什么。

这是我的代码:

AppComp

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

group: string;

changedvalue(event: Event) {
   const value = (<HTMLSelectElement>event.target).value;
   this.group = value;
   console.log('From AppComp' + this.group);
  }

其他组件

ts

@Input() group: string;

ngOnChanges() {
    console.log('Changed : ' + this.group);
  }

就像在浏览器控制台中一样,您可以看到有时它检测到更改,有时则没有。

未检测到更改:

enter image description here

检测到更改:

enter image description here

最佳答案

当输入模糊时调用更改函数。您可以像这样使用 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>

关于javascript - Angular : ngOnChanges is not detecting changes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53518539/

相关文章:

javascript - Dymo条码标签打印

javascript - 替换字符串开头为 >

javascript - Webpack/Angular - Materialise 未加载

typescript - 可观察变量的 Angular2 Observable.forkJoin - ReferenceError : Observable is not defined

typescript - 异步函数缺少返回语句?

javascript - ExtJS 列表数据 View

javascript - 打开新页面

javascript - Angular 2 Rollup Tree Shaking RxJS 库中的意外标记

angular - 如何隐藏/删除下划线输入 Angular Material?

typescript - 如何创建由一个账户签名但使用另一个账户支付的 Hedera 交易