I am using ng-repeat to create an array of items (a list), I would like to have an id against each item to be it's position in the array.
JS FIDDLE DEMO HERE: http://jsfiddle.net/m62Ez/15/
Please have a look at the following, looks like the ng-model="item.id" is never assign. Any suggestions much appreciated.
HTML
<div ng-controller="MainCtrl">
<button ng-click="addItem()">Add Item</button>
<ul>
<li ng-repeat="item in items">
<input type="hidden" ng-model="item.id" ng-value="{{$index}}" />
<input type="text" ng-model="item.name" />
</li>
</ul>
<hr />
<button ng-click="saveAll()">Save All</button>
</div>
JS
var app = angular.module("app", []);
app.controller("MainCtrl", function ($scope) {
$scope.items = [];
$scope.addItem = function () {
$scope.items.push({});
}
$scope.saveAll = function () {
console.log($scope.items); // item's do not have id's
}
});