Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJs: controller is called twice by using $routeProvider

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?

like image 389
Howard Avatar asked Jan 21 '13 16:01

Howard


2 Answers

I had face same issue today. I had added controller name in my $routeProvider and also in my html.

$routeProvider     .when('/login', {             controller: 'LoginController',             templateUrl: 'html/views/log-in.html'      }) 

and in my view as

<div class="personalDetails" ng-controller="LoginController"> </div> 

You can remove controller name either from your view or from your routeprovider.

like image 125
dip Avatar answered Sep 20 '22 16:09

dip


I had the same problem , and it seems there is a stupid bug with routing. There is some kind of redirection going on.

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.

like image 38
mpm Avatar answered Sep 20 '22 16:09

mpm