Angualr 1.4:this
.directive('counter', function counter() { return { scope: {},
restrict: 'EA',
transclude: true, bindToController: { count: '=' }, controller: function () { function increment() { this.count++; } function decrement() { this.count--; } this.increment = increment; this.decrement = decrement; }, controllerAs: 'counter', template: [ '<div class="todo">', '<input type="text" ng-model="counter.count">', '<button type="button" ng-click="counter.decrement();">-</button>', '<button type="button" ng-click="counter.increment();">+</button>', '</div>' ].join('') }; });
Angualr1.5:spa
.compoment('counter', { bindings: { count: '=' }, controller: function () { function increment() { this.count++; } function decrement() { this.count--; } this.increment = increment; this.decrement = decrement; }, controllerAs: 'vm', template: function($element, $attrs){ return [ '<div class="todo">', '<input type="text" ng-model="vm.count">', '<button type="button" ng-click="vm.decrement();">-</button>', '<button type="button" ng-click="vm.increment();">+</button>', '</div>' ].join(''); }, // restrict: 'E', // transclude: true });