Angular 2 Blur Event

Angular 2 blur event using the EventEmitter, a mother component receives the blur event when a user leaves an input from the child component.

angular 2 blur example
angular 2 blur example

live example and resources at the end of the article

try angular 2 blur
try angular 2 blur

 

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

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

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

         `]
})
class EventEmitterBlurComponent {
    @Output() eventEmitterBlur = new EventEmitter();

    eventEmitBlur(event) {
        this.eventEmitterBlur.emit(event);
    }

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

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

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

our example uses typescript as programming language.

angular 2 blur event