我有一个 Controller 执行两次的问题,但我看不到重复项(即使对我的文件运行区分大小写的搜索也只显示路由配置中的一个和我的实际 Controller )。
路由配置:
$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
}]);
密码重置.html
:
<div class="row">
<div class="small-12 columns">
<fieldset class="content-box">
password reset {{token}}
</fieldset>
</div>
</div>
此外,我创建了一条新路线并尝试过,但仍然记录了两次。我也清除了缓存。有的controller执行一次,有的执行两次(我在几个controller里放了一个alert,有的显示一次,有的显示两次)
另一个例子:
//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)
});
最佳答案
我发现了问题。我有两种布局,具体取决于用户是否使用 ng-show="user.authorized"
获得授权和 ng-show="!user.authorized"
.每个人都有一个 <div ng-view>
.每个ng-view
是否得到处理 ng-show
是真还是假。
我没有意识到 ng-show
中的一切无论是否为真都会被处理。
修复:使用 ng-switch
而不是 ng-show
.这可以防止在满足条件之前处理 View 。
关于javascript - angularjs Controller 执行 2 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17819911/