0

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));

}
4
  • use bind instead of model Commented Oct 16, 2015 at 21:09
  • ng-bind on the select elements? doesn't to work. Commented Oct 16, 2015 at 21:13
  • 1
    you add to array reference to object, so when you change select, change property this object, but not obect reference, so, for solving try copy object fields like: var ndata = {param:data.param, childParam:data.childParam} and then push to array ndata object instead data Commented Oct 16, 2015 at 21:15
  • @Grundy works perfectly... thanks...feel free to write an answer and I will mark it as correct. Or I will add it later. Commented Oct 16, 2015 at 21:17

1 Answer 1

1

You add to array reference to object, so when you change value in select, you change property existing object, but not object reference, so all reference point to object with updated field.
for solving you can copy object fields to new object, like:

var ndata = {param:data.param, childParam:data.childParam} 

and then push to array ndata object instead data

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.