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.
It's not all set-in-stone-rules, but you can use the following as a guidelines:
Separation of Concern
Does
doSomethingFancy
do something fancy with your model (on the scope)? -- Go with the controller.Does
doSomethingFancy
do something fancy with the user interface? -- Go with linkRace Conditions
This isn't entirely true: you can also use the
pre
link method.Common Sense
If you're code is more readably by placing a simple small function in the link method, use the link method. Over designing, just to comply with some rules isn't a good thing :) MV* patterns are guidelines.