My concern lies in the fact that the Controller
constructor seems to be executing multiple times. It appears that if I have 2 Directives
associated with a specific Controller
, it runs 2 + 1
times, and for 3 Directives
, it runs 3 + 1
times...and so on. This issue becomes even more problematic when there is an ajax request within the controller fetching data unnecessarily...
What could be causing this behavior? I worry about the excessive ajax requests being made...
HTML:
<span ng-controller="MenuController as MenuCtrl">
<main-menu></main-menu>
</span>
Controller:
var howManyTimes = 0;
angular.module("shredkit", ["myDirectives"])
.controller("MenuController", function(){
++howManyTimes;
console.log("HOW MANY TIMES? THAT MANY: " + howManyTimes )
})
Directive:
angular.module("myDirectives", [])
.directive("mainMenu", function(){
return{
restrict:"E", // type of directive - 'E' - element
templateUrl:'templates/main-menu.html',
controller:"MenuController",
controllerAs: "MenuCtrl"
};
})