ng-init in ng-repeat
Stckoverflow question : https://stackoverflow.com/questions/50211638/when-ng-init-in-ng-repeat-is-replays
by Nicolas Lips
HTML
<div ng-app="MyApp">
<ul ng-controller="MyController as ctrl">
<li ng-repeat="item in ctrl.getItems()">
<div ng-init="rank = $index">
[$index: {{$index}}]
{{item}}<br/>
<label>
Move to
<input type="number" ng-model="rank"/>
</label>
<button type="button" ng-click="ctrl.moveItem($index, rank)">
Ok
</button>
</div>
</li>
</ul>
</div>
JavaScript
angular
.module("MyApp", [])
.controller("MyController", [function () {
var items = [
"Item1",
"Item2",
"Item3"
];
this.getItems = function() {
return items;
};
this.moveItem = function(oldRank, newRank) {
var item = items[oldRank];
if (newRank < 0) {
newRank = 0;
}
if (newRank > items.length-1) {
newRank = items.length-1;
}
if (oldRank == newRank) {
return;
}
items.splice(oldRank, 1);
items.splice(newRank, 0, item);
}
}]);