The problem is that ng:repeat
creates a new scope, so you set selected
in the current scope, but the range is bound to the parent scope.
There are several solutions that define a method, possibly the best:
<div ng:controller="MyController"> <p>selected: {{selected.name}}</p> <ul> <li ng:repeat="artist in list"> <button ng:click="select(artist)" >{{artist.name}}</button> </li> </ul> </div>β
And the controller:
function MyController() { var scope = this; scope.select = function(artist) { scope.selected = artist; }; scope.list = [{ name: "Beatles", songs: ["Yellow Submarine", "Helter Skelter", "Lucy in the Sky with Diamonds"] }, { name: "Rolling Stones", songs: ["Ruby Tuesday", "Satisfaction", "Jumpin' Jack Flash"] }]; }β
Here is your example working on jsfiddle: http://jsfiddle.net/vojtajina/ugnkH/2/
Vojta source share