Search code examples
angularjsscopeparentcontrollerspartials

Correct use of parents scope properties


I'm really new on AngularJS and i don't know the best practices so here is my question.

What is the recommended use of parents controllers properties? Should I use different alias trough the entire webapp or there is another way to use vars and methods of a parent controller?

I'm using partials html and seems it can be confusing see userCtrl.doSomething when the partial has no declaration of userCtrl (because its declared on a partial thats includes this one).

Thanks in advance!


Solution

  • You should avoid to use $parent and scope variables that are not declared in your controller.

    Why ? Because if you use it, your controller will be directly dependent to where you instantiated it (ng-controller). So you won't respect the MVC pattern. Your controllers should not be dependent on the View.

    Your controller should not know what are his parents. Because you can do it doesn't means that you should to it.

    To share data between controllers, use Services instead. That's easier and cleaner.