AngularJs:使用 $routeProvider 调用控制器两次 [英] AngularJs: controller is called twice by using $routeProvider
本文介绍了AngularJs:使用 $routeProvider 调用控制器两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
模块路由:
var switchModule = angular.module('switchModule', []);
switchModule.config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/build-content', {templateUrl: 'build-content.html', controller: BuildInfoController});
}]);
控制器:
function BuildInfoController($http, $scope){
alert("hello");
}
HTML:
<html ng-app="switchModule">
...
<body>
<ul>
<li><a href="#build-content"/></a></li>
</ul>
<div class="ng-view"></div>
</body>
...
每次我单击超链接"时,BuildInfoController"都会被调用两次.我在这里遗漏了什么吗?
Each time when i click the hyperlink '', the 'BuildInfoController' will be called twice. Am i missing something here?
推荐答案
我遇到了同样的问题,而且路由似乎有一个愚蠢的错误.正在进行某种重定向.
I had the same problem , and it seems there is a stupid bug with routing. There is some kind of redirection going on.
为了修复它,我只是在 href 中添加了一个斜杠,例如:
to fix it , i just added a slash in the href , like :
<li><a href="#/build-content/"></a></li>
我希望它也能为您解决问题.
I hope it will fix things for you too.
这篇关于AngularJs:使用 $routeProvider 调用控制器两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文