I am injecting HTML with a ui-view into the DOM, then changing state using ui.router. Everytime the state is changed, that controller seems to be registered an additional time, thus the code in the controller executes multiple times. You can see a demonstration of this with the plnkr below. (just pop open the console, then click back and forth between the left and right button and notice that the controllers execute an additional time for each click.)
If the ui-views live on the index.html as static content then life is good, and the issue does not happen. It is only when injecting them into the DOM.
var app = angular.module('app', ["ui.router", "appHome", "appOther"]).config([
"$urlRouterProvider", "$stateProvider", "$locationProvider",
function ($urlRouterProvider, $stateProvider, $locationProvider) {
$stateProvider.state('appHome', {
url: '/home/index',
views: {
"Alerts": {
template: 'THINGS',
controller: "AlertsController as Alerts",
}
}
}).state('appOther', {
url: '/other/index',
views: {
"Other": {
template: 'THINGS',
controller: "OtherController as Other",
}
}
});
}
]);
Testcase on Plunker: http://plnkr.co/edit/M2wzVLSSgZ7naOi58qgL