It is important to remember that you are basically working with Javascript. No one would expect the following code to update the outer variable name:
1 2 3 4 5 6 7 |
var names = ["Misko", "Igor", "Vojta"]; function doubleName(name) { name = name + name; } for(var i=0;i<10;i++) { doubleName(names[i]); } |
This is because strings are passed by value in JavaScript. So this is equivalent to
1 2 3 4 5 6 |
<div ng-init='names = ["Misko", "Igor", "Vojta"]'> <div ng-repeat='name in names'> <div ng-init="name = name + name"></div> </div> {{names}} </div> |
Objects on the other hand are passed by reference so this does work:
1 2 3 4 5 6 7 |
var objs = [{ name: "Misko"}, {name: "Igor"}, {name:"Vojta"}]; function doubleName(obj) { obj.name = obj.name + obj.name; } for(var i=0;i<10;i++) { doubleName(obj[i]); } |
Which is equivalent to:
1 2 3 4 5 6 |
<div ng-init='objs = [{name:"Misko"}, {name:"Igor"}, {name:"Vojta"}]'> <div ng-repeat='obj in objs'> <div ng-init="obj.name = obj.name + obj.name"></div> </div> {{objs}} </div> |
Here is the Plnkr: http://plnkr.co/edit/ lmw14X?p=preview
There’s a typo in the third example. Missing a ‘{‘
Fixed! Thanks