46

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?

4
  • 5
    Does build-content.html have `ng-controller="BuildInfoController"? If so, try removing that. Commented Jan 21, 2013 at 16:34
  • No such an ng-controller like you said. Anyway, thanks a lot. I've already fixed it by using camus's solution. Commented Jan 22, 2013 at 1:40
  • 1
    possible duplicate of Combating AngularJS executing controller twice Commented Apr 19, 2015 at 11:15
  • I'm having a similar issue, but in my case the controller is being fired only on page refresh. If I navigate to the page from a link the controller only fires once, as expected. However if I refresh the page, it fires twice. Any suggestions? Commented Jul 13, 2015 at 9:31

10 Answers 10

72

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.

Sign up to request clarification or add additional context in comments.

Comments

42

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.

7 Comments

Thanks a lot. This weird problem almost killed me last night. ^_^
Nice one @mpm, you saved me a lot of time debugging
This solution stackoverflow.com/a/15535724/1387163 helps me to solve the same issue
Thanks for the answer. I was about loosing hope. Is this a known bug? I have a bunch of similar controllers and calls and there is only one I need to add a slash at the end of the URL. Happens only when called via ng-href not when the route is called directly.
thats not solve my problem.. my app is start and controller hit two time i am not click on any link..
|
16

I've had a similar problem. I found adding a trailing slash in the route but not in the link worked as expected.

$routeProvider.
when('/build-content/',...);

With this markup

<li><a href="/build-content">Content</a></li>

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!

2 Comments

You just saved my weekend!
Thanks heaps for this. We noticed exactly the same thing. We were careful to keep the routes and the hrefs consistent in terms of trailing slashes and could not get it to work. It was only once we followed your advice and made sure that they were different that it worked.
8

Mine was a case of having 2 ng-view directives. I tried to wrap it, but unintentionally duplicated it:

<div class="ng-view">
    <div ng-view></div>
</div>

Removed the wrapper, fixed it.

2 Comments

That happened to me before where I had two completely different app section in my index.html, one for my login/registration and another one for the app itself, both containing the ng-view.
@DominicGoulet I have two different ng-view in my index.html. so the controllers are being called twice.Is there any solution for that except removing ng-views. I don't want remove those 2 ng-views.
8

Remove the ng-controller directive from your template pages if exists .

2 Comments

It works!. May I know the reason, why should we remove ng-controller
Understood, already added in when('/trending/', { templateUrl: "./templates/trending.html", controller: 'TrendingController' }) , now it should not be in template
2

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 also angular.bootstrap(document,['app']) and that caused double initialization of controllers.

Hope this can save some time to someone.

Comments

1

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. :

  <div id="myDiv1" ng-controller="myController" ....></div>
  ....
  <div id="myDiv2" ng-controller="myController" ....></div>

Comments

0

I had declaration of SomeController for each view partial in a single state. That caused duplicate event firing.

 .state('chat', {
            url: '/',
            views: {
                'content@': {
                    templateUrl: 'views/chat_wv1.html',
                    controller: 'ChatController'
                },
                'form@': {
                    templateUrl: 'views/chat_wv_placeholder.html',
                    controller: 'ViewController'
                },
                'sidePanel@': {
                     templateUrl: 'views/sidePanel_wv.html'
                     /* removing this part solved my issue*/
                     /*controller: 'ChatController'*/
                }

            }
        })

Hope this helps some one else.

Comments

0

I too had a similar problem with a customDirective and unitentionally duplicated my controller.

<html>
   <body ng-app="MyApp" ng-controller="MyDirectiveCtrl">
      <my-directive></my-directive>
   </body>
</html>

angular.directive('myDirectivie', [function() {
   return {
         restrict: 'E',
         controller: 'MyDirectiveCtrl',

         ...

   }
}]);

I resolved it by removing ng-controller tag at body level

Comments

0

its because you have multiple ui-view

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.