As far as I know using controller property on 'Directive Definition Object' will create a separate instance of that controller everytime a given directive is linked?
Now playing with controllerAs patters I can see that when each of directives is being compiled the controller factory function is being triggered giving a different result for this.data.hello method.
But in my view I'm getting last instance of that controller. Why is that? What am I missing?
js
angular.module('app', [])
.controller('customCtrl', function () {
console.log('controller');
this.data = {
hello: Math.floor(Math.random() * 200 + 1)
};
})
.directive('customDrv', function () {
var linkFn = function (scope, element, attrs, ctrl) {
console.log('link');
console.log(ctrl.data.hello);
};
return {
templateUrl: 'Home/CustomDrv',
restrict: 'E',
controller: 'customCtrl',
controllerAs: 'vm',
compile: function (element, attrs) {
console.log('compile');
return linkFn
}
}
})
Html
<custom-drv></custom-drv>
<custom-drv></custom-drv>
<custom-drv></custom-drv>
<custom-drv></custom-drv>