admin管理员组

文章数量:1323150

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)
});

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)
});
Share edited Jul 24, 2013 at 13:33 Eric Shell asked Jul 23, 2013 at 19:59 Eric ShellEric Shell 9032 gold badges8 silver badges19 bronze badges 7
  • 1 Maybe this helps...stackoverflow./questions/15535336/… – veritasetratio 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. – Eric Shell 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 – Eric Shell 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. – Eric Shell 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. – Eric Shell Commented Jul 24, 2013 at 12:54
 |  Show 2 more ments

1 Answer 1

Reset to default 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.

本文标签: javascriptangularjs controller executes 2 timesStack Overflow