Skip to content

Instantly share code, notes, and snippets.

@jrwebdev
Last active November 16, 2016 05:46
Show Gist options
  • Select an option

  • Save jrwebdev/922094e4c97f3f634ce19a86f7ea3772 to your computer and use it in GitHub Desktop.

Select an option

Save jrwebdev/922094e4c97f3f634ce19a86f7ea3772 to your computer and use it in GitHub Desktop.
Angular 1 vs Angular 2 Output Bindings
// 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