This is due to the fact that when using ng-repeat
each template instance gets its own scope and due to how prototypal inheritance works in JavaScript.
In short, when you click a li
element it will create the currentThing property on the newly created child scope belonging to the template, instead of updating currentThing
of the parent.
If you use this instead and click the second li
you will notice that it updates its own scope:
<li ng-click="currentThing=thing" ng-repeat="thing in things">
<pre>{{ currentThing | json }}</pre>
</li>
Demo: http://jsfiddle.net/A3eCe/
The recommended way to get around this is to use an object, for example:
<div ng-app ng-init="viewModel = { things: [ { name: 'one', num: 1 }, { name: 'two', num: 2 } ] }; viewModel.currentThing = viewModel.things[0]">
<p>name: {{ viewModel.currentThing.name}} num: {{ viewModel.currentThing.num}}</p>
<ul>
<li ng-click="viewModel.currentThing = thing" ng-repeat="thing in viewModel.things">{{thing.name}}</li>
</ul>
</div>
Demo: http://jsfiddle.net/4suvy/
An excellent explanation on the issue can be found here.