typescript - 什么是 Angular 2 中的 `change` 事件

标签 typescript angular

什么是 Angular 2 中的 change 事件?什么时候发货,如何使用?
IE。我通过 (change)="update()" 在以下代码中订阅了什么?

http://plnkr.co/edit/mfoToOSLU6IU2zr0A8OB?p=preview

import {Component, View, Input, Output, EventEmitter, OnChanges} from '@angular/core'

@Component({
  selector: 'inner-component',
  template: `
    <label><input type="checkbox" [(ngModel)]="data.isSelected"> Selected</label>
  `
})
export class InnerComponent {
  data = { isSelected: false };
}

@Component({
  selector: 'my-app',
  template: `
    <p><inner-component (change)="update()"></inner-component></p>
    <p>The component was updated {{count}} times</p>
  `,
  directives: [InnerComponent]
})
export class AppComponent {
  count = 0;

  update() {
    ++this.count;
  }
}

附言:Same question in Russian .

最佳答案

这就是事件冒泡:change 发生在 input 上,然后通过 dom 树冒泡并在 inner-component 元素上得到处理。可以通过记录事件来检查它:

http://plnkr.co/edit/J8pRg3ow41PAqdMteKwg?p=preview

@Component({
  selector: 'my-app',
  template: `
    <p><inner-component (change)="update($event)"></inner-component></p>
    <p>The component was updated {{count}} times</p>
  `,
  directives: [InnerComponent]
})
export class AppComponent {
  count = 0;

  update($event) {
    console.log($event, $event.target, $event.currentTarget);
    ++this.count;
  }
}

关于typescript - 什么是 Angular 2 中的 `change` 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38227838/

相关文章:

angular - FormBuilder 组已被验证器弃用

c# - 大数据量的 Web API 中的 502 错误

angular - Jenkins-错误 : Cannot find module 'typescript'

Angular 属性指令表单输入

html - 禁用类 ="img-responsive"时, Bootstrap 列重叠

javascript - TypeScript 中的 Soap 请求

javascript - typescript 从包含 url 的字符串中获取主机名、协议(protocol)、端口

ios - Nativescript - 引用错误 : FIRAuth

typescript - 如何使用 `sequelize-typescript` 定义表名?

css - 子组件未占用 100% 的空间( Angular Material 选项卡)