3

One can define controller for the state in ui router as controller: 'MainController', but also this can be omitted, and instead controller can be defined in view (template url) as ng-controller="MainController", what is the better practice?

1 Answer 1

1

The better practice is to define the controller in the provider configuration (this also works with the core $routeProvider btw). It's best to keep your view as 'clean' as possible.

As @JBNizet added, this is also the only way for the router to inject the resolved dependencies into the controller (see the resolve property that you can pass into your route/state config)

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

8 Comments

But more importantly, that's the only way for the router to injected the resolved dependencies to the controller.
Correct, I'll add it to to my answer
I have an Angular app in which my controller is defined by my routeProvider, however, in the associated view, I have references to scope member variables so the view still feels tightly coupled to my controller. Am I doing something wrong?
@Drumbeg Referencing to scope member variables isn't wrong no
Cheers @NexusDuck. I realised a while back that my view is actually referencing a model, not a controller, which is more in line with how I picture MVC to work.
|

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.