Combating AngularJS executing controller twice

I understand AngularJS runs through some code twice, sometimes even more, like $watch events, constantly checking model states etc.

However my code:

function MyController($scope, User, local) {

var $scope.User = local.get(); // Get locally save user data

User.get({ id: $scope.User._id.$oid }, function(user) {
  $scope.User = new User(user);
  local.save($scope.User);
});

//...

Is executed twice, inserting 2 records into my DB. I'm clearly still learning as I've been banging my head against this for ages!

Ok figured it out with some guidance from Stewie in the comments.

My app has routing, and it was navigating to MyController like so:

$routeProvider.when('/',
                   { templateUrl: 'pages/home.html',
                     controller: MyController });

But I also had this in home.html:

<div data-ng-controller="MyController">

Which was instructing AngularJS to digest my controller twice.

To solve it I simply removed the data-ng-controller from my HTML.

Obviously really when I think about it!

Note: If you want you could remove the controller: property from the routing and keep the ngController directive in your HTML. Either way Angular will now only execute your controller once.

AngularJS docs - ngController
Note that you can also attach controllers to the DOM by declaring it in a route definition via the $route service. A common mistake is to declare the controller again using ng-controller in the template itself. This will cause the controller to be attached and executed twice.

When you use ngRoute with the ng-view directive, the controller gets attached to that dom element by default (or ui-view if you use ui-router). So you will not need to attach it again in the template.

Just want to add one more case when controller can init twice (this is actual for angular.js 1.3.1):

<div ng-if="loading">Loading...</div>
<div ng-if="!loading">
    <div ng-view></div>
</div>

In this case $route.current will be already set when ng-view will init. That cause double initialization.

To fix it just change ng-if to ng-show/ng-hide and all will work well.

I just went through this, but the issue was different from the accepted answer. I'm really leaving this here for my future self, to include the steps I went through to fix it.

  1. Remove redundant controller declarations
  2. Check trailing slashes in routes
  3. Check for ng-ifs
  4. Check for any unnecessary wrapping ng-view calls (I accidentally had left in an ng-view that was wrapping my actual ng-view. This resulted in three calls to my controllers.)
  5. If you are on Rails, you should remove the turbolinks gem from your application.js file. I wasted a whole day to discover that. Found answer here.

In some cases your directive runs twice when you simply not correct close you directive like this:

<my-directive>Some content<my-directive>

This will run your directive twice. Also there is another often case when your directive runs twice:

make sure you are not including your directive in your index.html TWICE!

When using angular-ui-router with Angular 1.3+, there was an issue about Rendering views twice on route transition. This resulted in executing controllers twice, too. None of the proposed solutions worked for me.

However, updating angular-ui-router from 0.2.11 to 0.2.13 solved problem for me.

I tore my app and all its dependencies to bits over this issue (details here: AngularJS app initiating twice (tried the usual solutions..))

And in the end, it was all Batarang Chrome plugin's fault.

Resolution in this answer:

I'd strongly recommend the first thing on anyone's list is to disable it per the post before altering code.

Would like to add for reference:

Double controller code execution can also be caused by referencing the controller in a directive that also runs on the page.

e.g.

return {

            restrict: 'A',
            controller: 'myController',
            link: function ($scope) { ....

When you also have ng-controller="myController" in your HTML

Been scratching my head over this problem with AngularJS 1.4 rc build, then realised none of the above answers was applicable since it was originated from the new router library for Angular 1.4 and Angular 2 at the time of this writing. Therefore, I am dropping a note here for anyone who might be using the new Angular route library.

Basically if a html page contains a ng-viewport directive for loading parts of your app, by clicking on a hyperlink specified in with ng-link would cause the target controller of the associated component to be loaded twice. The subtle difference is that, if the browser has already loaded the target controller, by re-clicking the same hyperlink would only invoke the controller once.

Haven't found a viable workaround yet, though I believe this behaviour is consistent with the observation raised by shaunxu, and hopefully this issue would be resolved in the future build of new route library and along with AngularJS 1.4 releases.

In my case it was because of the url pattern I used

my url was like /ui/project/:parameter1/:parameter2.

I didn't need paramerter2 in all cases of state change. In cases where I didn't need the second parameter my url would be like /ui/project/:parameter1/. And so whenever I had a state change I will have my controller refreshed twice.

The solution was to set parameter2 as empty string and do the state change.

If you know your controller is unintentionally executing more than once, try a search through your files for the name of the offending controller, ex: search: MyController through all files. Likely it got copy-pasted in some other html/js file and you forgot to change it when you got to developing or using those partials/controllers. Source: I made this mistake

In my case, I found two views using the same controller.

$stateProvider.state('app', {
  url: '',
  views: {
    "viewOne@app": {
      controller: 'CtrlOne as CtrlOne',
      templateUrl: 'main/one.tpl.html'
    },
    "viewTwo@app": {
      controller: 'CtrlOne as CtrlOne',
      templateUrl: 'main/two.tpl.html'
    }
  }
});

I had the same problem, in a simple app (with no routing and a simple ng-controller reference) and my controller's constructor did run twice. Finally, I found out that my problem was the following declaration to auto-bootstrap my AngularJS application in my Razor view

<html ng-app="mTest1">

I have also manually bootstrapped it using angular.bootstrap i.e.

angular.bootstrap(document, [this.app.name]);

so removing one of them, it worked for me.