Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is `change` event in Angular 2

What is change event in Angular 2? When is it dispatched and how can I use it?
I. e. what have I subscribed in following code via (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;
  }
}

PS: Same question in Russian.

like image 630
Qwertiy Avatar asked Jul 06 '16 15:07

Qwertiy


People also ask

What is change event in Angular?

NgModelChange is an Angular specific event, which we can use to listen for changes to the user input. It is the @Output property of the ngModel directive, Hence we need to use it along with it. ngModle raises the NgModelChange event, whenever the model changes.

How does angular 2 detect change?

Angular 2's change detection system is built on top of zone. js hooks. Once an asynchronous action completes, Angular 2 starts its change detection routine. This means traversing all of the nodes in the "component tree" always starting with the root node.

What changed in angular 2?

Changes in Data Binding Like in Angular 1 ng-bind is used for one-way data binding and ng-model is used for two-way data binding. Angular 2 replaced them with [property], as Angular 2 directly uses valid HTML DOM element properties and events so the built-in directives of Angular 1 are no longer required.

What is the difference between change vs NgModelChange?

“ (change) is bound to the HTML onchange event.” In simple terms, this event will be triggered for any changes that occur to the element. “ (ngModelChange) is bound to the ngmodel which we added to the element.” In simple terms, this event will be triggered for any changes that occur to the ngmodel variable.


2 Answers

The change event notifies you about a change happening in an input field. Since your inner component is not a native Angular component, you have to specifiy the event emitter yourself:

@Component({
  selector: 'inner-component',
  template: `
    <label><input type="checkbox" (change)="inputChange.emit($event)" [(ngModel)]="data.isSelected"> Selected</label>
  `
})
export class InnerComponent {
  @Output('change') inputChange = new EventEmitter();

  data = { isSelected: false };
}

And in your AppComponent you're now receiving the events:

@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: any) {
    ++this.count;
    console.log(event);
  }
}
like image 141
rinukkusu Avatar answered Oct 06 '22 18:10

rinukkusu


That's event bubbling: change occures on input, then bubbles by dom tree and gets handled on inner-component element. It can be checked by logging an event:

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;
  }
}
like image 20
Qwertiy Avatar answered Oct 06 '22 19:10

Qwertiy