I am using angularjs.I am having one select box.In select i am displaying all the exercise names by doing ng-repeat where multiple exercise names can be selected.
Here is my HTML
<div class="form-group">
<label for="exampleInputEmail1">Selct Exercises</label>
<select class="form-control select2" multiple>
<option data-ng-repeat="exercise in exercises" value="
{{exercise}}" data-ng-checked="selection.indexOf(exercise) > -1" data-ng-click="toggleSelectionExercise(exercise)">
{{exercise.name}}</option>
</select>
</div>
Now after selecting multiple exercise names onclick i am calling this function
Controller.js
$scope.parameterTest = [];
$scope.parameter1 = [];
var exercise = [];
$scope.toggleSelectionExercise = function
toggleSelection(exercise) {
var idx = $scope.parameterTest.indexOf(exercise);
if (idx > -1) {
$scope.parameterTest.splice(idx, 1);
} else {
console.log(exercise);
$scope.parameterTest.push(exercise);
for (var i = 0; i < $scope.parameterTest.length; i++) {
var parameter1 = {
"exid": exercise._id,
"modify": 0,
"sets": exercise.sets,
"reps": exercise.reps,
"rest_duration": 60,
"ord": 0
};
/*Setting in json format and storing in parameter1*/
$scope.parameterTest[i] = parameter1;
/*parameter1 value passing to $scope.parameterTest[i]*/
console.log($scope.parameterTest[i]);
}
}
}
I am passing exercise object to the function.From the exercise object I am setting values and passing in JSON format and storing it in variable parameter1.
I want to set every object selected in dropdown to be set in this format thats why I am iterating every object and setting the format and value.
But now if I select one option from dropdown it is displaying that object and setting the correct value and format.But if i select multiple options then for 2 options it is displaying like this
First object value set in JSON Format
Object {exid: "59bc2dbc53bdcb7171732eab", modify: 0, sets: 3, reps: Array[6], rest_duration: 60…}
Second object value set in JSON Format
Object {exid: "59bc2d9453bdcb7171732eaa", modify: 0, sets: 3, reps: Array[6], rest_duration: 60…}
Second option displaying second time
Object {exid: "59bc2d9453bdcb7171732eaa", modify: 0, sets: 3, reps: Array[6], rest_duration: 60…}
If I select two options it is displaying first object once and second object twice.
I dont know where I am doing wrong. Am I doing looping correctly?