Wednesday, June 4, 2014

Binding to an Array

What if you want to bind to an array and have html elements repeated for each item in the array? This is easy with ng-repeat. Before that we need to initialize some data in our model. Although we’d do this in a controller, but as an initial demo we’re going to use the rarely used ng-init directive to add an array to our model. ng-init lets you execute angular expressions when the page first loads. Doing this with controller will be discussed after this

Advanced Tangent: Notice that we have a “user” on our model from the Hello World sample above, and yet inside the ng-repeat, the correct “user” object–from the users array–was used even though there was some ambiguous use of the term user. This is because the ng-model directive uses it’s own isolated scope. Don’t worry too much about this, just keep it in the back of your mind.

No comments:

Post a Comment