4

I have a problem where a controller executes twice, yet I do not have duplicates that i can see (even running a case sensiTIve search through my files only shows one in route configuration and my actual controller).

Route config:

$routeProvider.when('/cb159e1ec61d0613f10ab397d8bd6782',{
    templateUrl: view+'passwordreset.html',
    controller:'passwordreset'
});

Controller:

App.controller('passwordreset',['$scope','$http',
    function($scope,$http){
        $scope.token='asdf'
        console.log($scope.token); // <-- logs 2x to console
}]);

passwordreset.html:

<div class="row">
    <div class="small-12 columns">
        <fieldset class="content-box">
            password reset {{token}}
        </fieldset>
    </div>
</div>

Also, I've created a new route and tried, still logs twice. I've cleared my cache as well. Some controllers execute once, some twice (I put an alert in a few controllers, some show once, some twice)

another example:

//route config
    $routeProvider.when('/', {
     templateUrl: view+'home',
     controller:'Home'
    });

//template   
        <div ng-controller="testing">a</div>
//controller
Lmu.controller('Home',function($scope){
    $scope.home='home';
    alert($scope.home); <-- alerts 2x (two scopes for Home controller are created)
});
Eric Shell
  • 913
  • 2
  • 8
  • 19
  • 1
    Maybe this helps...http://stackoverflow.com/questions/15535336/combating-angularjs-executing-controller-twice – veritasetratio Jul 23 '13 at 21:27
  • i checked to make sure my controller wasn't being called twice. i only see two instances of the controller when running a search through my files (1. the controller itself, 2. in my route config). this also is happening in a few other controllers. when I run angularjs-batarang tool, it shows two separate scopes are being created for the controller. – Eric Shell Jul 24 '13 at 03:49
  • if i add ng-controller to my view as well as the route config, the scope is duplicated 4 times. ex: Scope00G – Eric Shell Jul 24 '13 at 04:00
  • even when i have multiple controllers called in one view, some of them get called once properly, some twice. these are controllers only called by ng-include in the html template. – Eric Shell Jul 24 '13 at 12:50
  • if i remove the ng-controller getting executed twice from the view, nothing happens. I put it back, controller gets ran twice. i'm 100% i'm not calling the controller twice. – Eric Shell Jul 24 '13 at 12:54
  • using chrome dev tools.. when this controller performs an $http.get, the http request is only being performed once under the network tab, and the script being ran via http.get is only being executed once in the console via chromephp output. but console shows xhr request performed twice to http.get's location, and if i put an 'alert()' in the controller, it alerts twice... – Eric Shell Jul 24 '13 at 13:21
  • I have been having the same problem Eric and I also have no repeated controllers. – Rezen Sep 03 '13 at 17:54

1 Answers1

8

I figured out the problem. I had two layouts depending on whether the user was authorized using ng-show="user.authorized" and ng-show="!user.authorized". Each had a <div ng-view>. Every ng-view gets processed whether ng-show is true or false.

I didn't realize everything in ng-show gets processed whether true or not.

Fix: used ng-switch instead of ng-show. This keeps the view from being processed until the condition is met.

user3335966
  • 2,673
  • 4
  • 30
  • 33
Eric Shell
  • 913
  • 2
  • 8
  • 19