Dave
Dave

Reputation: 1502

Why alias is used for ng-controller in Angular?

ng-controller="StoreController as store"

instead of

ng-controller="StoreController"

What is the difference?

Upvotes: 3

Views: 5433

Answers (2)

Kwaye Kant
Kwaye Kant

Reputation: 57

Additionally, an alias is like an instance in POO. By doing this you are sure to be accessing only the StoreController scope in you code.

You can get controllers(child) which are shadowed in another controller(parent) with the same scope 'id'. Doing this {{ id }} will display the id of the parent.

Using an alias this becomes

controllers(child) as child
controller(parent) as parent

and child.id gives the id of the child and parent.id displays the id of the parent.

Hope this helps

Upvotes: 1

Cedric
Cedric

Reputation: 1256

Please refer to this link as to know why they add an alias or specifically a scope for Angular. It's more of a clearer namespaces vs confusing namespaces

Upvotes: 2

Related Questions