Angular 2 Change Event

Angular 2 change event using the EventEmitter, a mother component receives the change event from the child component when a content of an element (input, textarea) is changed.

angular 2 change event example
angular 2 change event example

Try it yourself,Live Example

try angular 2 change event
try angular 2 change event
/**
 * Created by Tareq Boulakjar. from angulartypescript.com
 */
import {Component,EventEmitter,Output} from 'angular2/core';

/*Child Component Will (Send or Emit) a Change Event */
@Component({
    selector: 'event-emitter-change-com',
    template:`
                <h2>EventEmitter on Change</h2>
                <input type="text" value="Do Change Action To See" (change)="eventEmitChange($event)" class="event-emitter-change-component-style"/>

    `,
    styles:[`
            .event-emitter-change-component-style {
              width: 200px;
              background-color:#00AA00;
              color: white;
             }

         `]
})
class EventEmitterChangeComponent {
    @Output() eventEmitterChange = new EventEmitter();

    eventEmitChange(event) {
        this.eventEmitterChange.emit(event);
    }

}
/*Mother is the receiver of the event */

@Component({
    selector: 'event-emitter-change',
    template: `
                <event-emitter-change-com (eventEmitterChange)="receiveEvent($event)" >
                </event-emitter-change-com>
               `,
    directives: [EventEmitterChangeComponent]

})
export class EventEmitterChangeExample {
    receiveEvent(event) { console.log('Change event from angulartypescript.com.'); console.log(event);  }
}

Our example uses Typescript as programming language.

Done !