Module routes:
var switchModule = angular.module('switchModule', []);
switchModule.config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/build-content', {templateUrl: 'build-content.html', controller: BuildInfoController});
}]);
Controller:
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>
...
Each time when i click the hyperlink '', the 'BuildInfoController' will be called twice. Am i missing something here?
I had same problem and found that if you bootstrapped you angular two time you can have same error.
In my case I had
<body ng-app>
but alsoangular.bootstrap(document,['app'])
and that caused double initialization of controllers.Hope this can save some time to someone.
Same Here Mine was a case of having 2 ng-view directives.
enter code here
Removed the duplicate i.e. ng-view, fixed it.I had face same issue today. I had added controller name in my $routeProvider and also in my html.
and in my view as
You can remove controller name either from your view or from your routeprovider.
A controller can be added to more than one element of the DOM, so this problem can occur if this has been done e.g. :
I've had a similar problem. I found adding a trailing slash in the route but not in the link worked as expected.
With this markup
And then AngularJS will correct the URL in the browser to what is defined in the $routeProvider.
Bizarrely the opposite seems to work too with a trailing slash in the link and not in the route. It seems as long as the trailing slashes don't match the resolves and controller won't be called twice!
Remove the
ng-controller
directive from your template pages if exists .