Objects i've named touts are being delete in order of creation not by id
The function to delete them is called deleteTouts()
Heres a live code:
jsFiddle link
HTML
<div ng-app="App">
<div ng-controller="Controller" class="container">
<button ng-click="addFields()">
Add Field
</button>
<hr />
<hr />
<div class="row" ng-repeat="(keyField, keyVal) in fields">
<input ng-hide="true" value="field" ng-model="field.id" />
<div>{{keyVal.id}} -- <span><a href="" ng-click="removeFields($index)">delete</a></span></div>
<div class="" ng-repeat="(keyTout, valTout) in fields[keyField].touts">
<div class="captions">
<input value="field" ng-model="valTout.caption" />
<input value="field" ng-model="valTout.subCaption" />
<br/>
</div>
<button ng-click="deleteTouts(keyTout)">delete tout</button>
</div>
<button ng-click="addTouts(keyField)">add tout</button>
<hr />
</div>
<hr />
<p class="well">fields: {{fields | json}}</p>
</div>
</div>
JS
angular.module('App', []);
function Controller($scope) {
$scope.friends = [];
$scope.fields = [
//'id': 0, touts: [{'caption': 'default caption', 'subCaption': 'default subcaption'}]
];
$scope.addFields = function() {
var idFields = $scope.fields.length == undefined ? 0 : $scope.fields.length ;
$scope.fields.push({id: idFields, touts: [{'caption': 'default caption', 'subCaption': 'default subcaption'}]});
};
$scope.removeFields = function(id) {
$scope.fields.splice( $scope.fields.indexOf(id), 1 );
};
$scope.addTouts = function (id) {
//alert(id);
$scope.fields[id].touts.push({'caption': 'default caption', 'subCaption': 'default subcaption'});
}
$scope.deleteTouts = function (id) {
//alert(id);
$scope.fields[id].touts.splice( $scope.fields[id].touts.indexOf(id), 1 );
}
}
DIRECTIONS:
Create 2 fields ->
create 2 touts in field[0] ->
create 1 tout in field[1]
Now why when you delete from field[1]tout[0] using delete tout
it deletes the first tout made.
This functionality is incorrect. lastly is there a better method of adding/subtracting? (directive)?
deleteTouts()method with the argument keyTout. I do not see where keyTout is defined anywhere in the app. As such wouldn't it be undefined, essentially 0?