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)
});
7
  • 1
    Maybe this helps...stackoverflow.com/questions/15535336/… Commented Jul 23, 2013 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. Commented Jul 24, 2013 at 3:49
  • if i add ng-controller to my view as well as the route config, the scope is duplicated 4 times. ex: Scope00G <Scope00H Scope00I <Scope00J Commented Jul 24, 2013 at 4: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. Commented Jul 24, 2013 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. Commented Jul 24, 2013 at 12:54

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Leaving breadcrumbs back to my steps to fix this for my future self.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.