Last active
November 16, 2016 05:46
-
-
Save jrwebdev/922094e4c97f3f634ce19a86f7ea3772 to your computer and use it in GitHub Desktop.
Angular 1 vs Angular 2 Output Bindings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Angular 1 | |
| const module = angular.module('myModule', []); | |
| module.component('myComponent', { | |
| bindings: { | |
| buttonClick: '&' | |
| }, | |
| controller() { | |
| this.clickCount = 0; | |
| this.onButtonClick = () => { | |
| this.buttonClick({clicks: ++this.clickCount}); | |
| }; | |
| }, | |
| template: ` | |
| <div> | |
| <button ng-click="$ctrl.onButtonClick()>Click Me!</button> | |
| </div> | |
| ` | |
| }); | |
| // Usage | |
| // <my-component button-click="$ctrl.onButtonClick(clicks)"></my-component> | |
| /***************************************************************/ | |
| // Angular 2 | |
| import {Component, Output, EventEmitter} from '@angular/core'; | |
| @Component({ | |
| selector: 'my-component', | |
| template: ` | |
| <div> | |
| <button ng-click="onButtonClick()>Click Me!</button> | |
| </div> | |
| ` | |
| }) | |
| class MyComponent { | |
| @Output() buttonClick: EventEmitter<number> = new EventEmitter(); | |
| private clickCount: number = 0; | |
| onButtonClick() { | |
| this.buttonClick.emit(++this.clickCount); | |
| } | |
| } | |
| // Usage | |
| // <my-component (buttonClick)="onButtonClick($event)"></my-component> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment