1

I've made an app with 3 slightly different views which use the same logic, so each one uses the same controller, but have different json data. Everything is working fine, but I was wondering if it is AngularJS best practice to do so ? When routes change, does the view replaced uses the same instance of the controller, or does it instantiate a new controller ?

Thanx.

joe cool
  • 315
  • 7
  • 14
  • In my small experience, each time a view changes, its controller was reloaded. I'm using ui-router, so maybe AngularJs basic routes don't have the same behavior. You can put "console.log" in your controller to detect this. – Neozaru Dec 10 '13 at 15:55
  • I wondered the same about here is some helpful info [Angular Best Practices](http://stackoverflow.com/q/20802798/1959948) – Dalorzo May 17 '14 at 22:43

1 Answers1

1

A new controller is created each time a view is loaded--controllers are transient in this sense. If state needs to be maintained, you should use a service or factory.

I personally will share controllers between different variations of the same view. In a general sense, if the controller represents the same logic (or abstraction) in relation to the view, I see no reason not to re-use it.

Phil Sandler
  • 27,544
  • 21
  • 86
  • 147