I'm using two <select> tags to push values to an array in the $scope. For some reason this array then becomes connected to the select elements and when they are changed it changes the array elements.
I have made a codepen to demonstrate this behaviour.
View:
<div class="form-group">
<div class="row">
<div class="col-sm-4">
<label class="item item-input item-select">
<div class="input-label positive">
Select Parameter
</div>
<select ng-model="data.param">
<option ng-repeat="param in params track by $index" value="{{param}}">{{param}}</option>
</select>
</label>
</div>
<div class="col-sm-4">
<label class="item item-input item-select">
<div class="input-label positive">
{{data.param || 'SELECT'}}
</div>
<select ng-model="data.childParam">
<option ng-repeat="child in children[data.param] track by $index" value="{{child}}">{{child}}</option>
</select>
</label>
</div>
<div class="col-sm-4">
<button class="btn btn-primary" ng-click="addParam(data)"> SAVE</button>
</div>
</div>
<ul class="list-group">
<li ng-repeat="savedParm in activeExercise.Params track by $index" class="list-group-item"><strong>{{savedParm.param}}</strong> : {{savedParm.childParam}}
</li>
</ul>
{{activeExercise.Params}}
</div>
Controller:
$scope.addParam = function(data) {
console.log(data);
if (!$scope.activeExercise.Params) {
$scope.activeExercise.Params = [];
}
if ($scope.activeExercise) {
$scope.activeExercise.Params.push(data);
} else if ($scope.editExercise.Params) {
$scope.editExercise.Params.push(data);
}
console.log(JSON.stringify($scope.activeExercise));
}
ng-bindon the select elements? doesn't to work.var ndata = {param:data.param, childParam:data.childParam}and then push to arrayndataobject insteaddata