After injecting HTML with a ui-view into the DOM and changing state using ui.router, I noticed that the controller gets registered multiple times whenever the state is changed. This results in the code executing multiple times, as demonstrated in the Plunker link below. Simply open the console and click back and forth between the left and right buttons to see the controllers executing an additional time for each click.
The issue only occurs when the ui-views are injected into the DOM, not when they live on the index.html as static content.
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",
}
}
});
}
]);
Check out the testcase on Plunker here: http://plnkr.co/edit/M2wzVLSSgZ7naOi58qgL