1

All,

We are developing our app with AngularJS and time and again we keep running into the issue of Angular running or loading controllers twice. This becomes a little more intrusive when testing our controllers and more specifically when working with Testacular with jasmine's SpyOn's (since they get triggered before our code runs). So, our question is, is there such a thing as a constructor or init method that Angular is guaranteed to call when instantiating the controllers w/o having to hack work-arounds in the test code? TIA.

2
  • It might help to see how your tests are set up, since during testing Angular should really just be instantiating the controller once. Commented Nov 12, 2012 at 17:22
  • 2
    I don't think it will be easy to help without seeing code examples... Commented Nov 12, 2012 at 17:25

2 Answers 2

8

If you are specifying the controller in your router, then your template doesn't need to specify the controller via an ng-controller tag. Doing so will double load your controller.

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

2 Comments

This has also happened to me. The Angular.js docs should be much more clear about this, but looking at the diff of their sample app shows the removal of the controller info from the template after routes were added: github.com/angular/angular-phonecat/compare/step-6...step-7
I had the same problem and I was doing what you described. Thanks, I was banging my head against the wall!
1

Your controllers shouldn't be loaded twice, unless you are doing something wrong. You shouldn't have to hack any work-arounds.

You might want to provide a concrete example of how you are loading the controllers (and/or the partials which are associated with the controllers). It sounds like you probably have a routing issue which is causing your view to be loaded twice for each request.

Assuming that is a routing-related issue, unless/until you can provide more information to help people to help you, you may want to read this document. Pay particular mention to sections that mention 'redirect' and 'HTML 5' mode:

http://docs.angularjs.org/guide/dev_guide.services.$location

With so little information to go on, I can't answer your question, but perhaps that link will help you to help yourself. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.