AngularJS Architecture: Prefer the 'controller as' syntax

InstructorLukas Ruebbelke

Share this video with your friends

Send Tweet

As of Angular 1.2, the use of $scope injectable has been greatly reduced in favor of the "controller as syntax". We are going to take a quick look at cleaning up Eggly to follow this best practice.

mifprojects
mifprojects
~ 6 years ago

Hello Lukas! I want to ask you, is there a good way to use some variable from parent state ctrl in child state ctrl using the controller as syntax? For example using $scope way I have scope inheritance so it works fine, but how it could be done using new controller as syntax? Thanks!

Lukas Ruebbelke
Lukas Ruebbelke(instructor)
~ 6 years ago

Hello -- I recommend checking out Todd Motto's excellent article on 'controller as' syntax here http://toddmotto.com/digging-into-angulars-controller-as-syntax/. Check out the section on nested scopes as he locks it down perfectly from the template side.

In terms of JavaScript, I would promote that variable to a service and share it between the two controllers which I cover in the next lesson. I would do my best to avoid implicit inheritance as it couples your two controllers together which makes extensibility and testing really hard.

Does this make sense?

mifprojects
mifprojects
~ 6 years ago

Thanks for answering. I think you are right and it make sense. Will wait next videos! Thanks :)

Frank Stepanski
Frank Stepanski
~ 6 years ago

I did the same code, but do not get the updated information in the <H1>..any ideas?

http://angularjs.javascriptworkshop.com/lesson7/step13/index.html#/categories/Development

Frank Stepanski
Frank Stepanski
~ 6 years ago

nevermind....dumb caching :)