Angular and Scope - or - How not to waste time figuring out why a scope value is not "working"

Posted: 2014-04-30 17:09:07

The creator of Angular notes it in this video which is well worth the watch.

Basically when using includes or directives etc you may not be getting a scope value as easily because it is child scope and you may be looking in the parent scope.

For example

This include if I have an include and it references a model attribute like this

//Some include
<input ng-model="firstname">

I may not get the results from that input that I expect. I may not get any. So to be safe it is best to make an Object for your data that then has keys to get the value from. For example

//Some controller
$scope.person = {};
$scope.person.firstname = "Bob"

Then back in that include

//Some include
<input ng-model="person.firstname">

Tends to work better than fighting with $parent prefixes as such

//Some include
<input ng-model="$parent.firstname">

Anyways glad I watched the video which meant I only wasted under an hour one this head scratcher.