When using the controllerAs syntax in AngularJS, what is the best place to define handlers for ng-click and such? On the controller or on the scope (defined in the link function)?
So, do you use:
angular.module('app', []).
controller('myDirective', function(){
return {
template: '<div><button ng-click=vm.onClick()></button></div>',
controller: function(){
var vm = this;
vm.onClick = function(){
vm.myValue = vm.doSomethingFancy();
};
this.doSomethingFancy = function(){};
}
}
});
Or:
angular.module('app', []).
controller('myDirective', function () {
return {
template: '<div><button ng-click=onClick()></button></div>',
require: 'myDirecive',
controller: function () {
var vm = this;
this.doSomethingFancy = function () {
vm.myValue = 'somethingfancy';
};
},
link: function (scope, elem, attr, ctrl) {
scope.onClick = function () {
ctrl.doSomethingFancy();
}
}
}
});
I actually like the second one because now the controller is only assigning values and event handling is done in the link function.
Anyway...let me know what your thoughts are.