I've got an array of values that i show with an ng-repeat. When i click over one of it, i add this value in another array. If already exists i remove it. It works well here. But i have a button that push all array in the second. It's working but i can push the whole array infite times even if a value already exists. Of course, if i check one or two value and then i push "Select all" it must select all values also the values already select with the single selection. By thge way this is the code with a jsfiddle:
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
$scope.all_titles = [
"Title 1",
"Title 2",
"Title 3",
"Title 4"
];
$scope.selection=[];
$scope.getSelectedItem = function getSelectedItems(title) {
var idx = $scope.selection.indexOf(title);
// is currently selected
if (idx > -1) {
$scope.selection.splice(idx, 1);
}
// is newly selected
else {
if(Array.isArray(title)) {
for(var i=0;i<title.length;i++) {
$scope.selection.push(title[i]);
}
} else {
$scope.selection.push(title);
}
}
};
}
<div ng-controller="MyCtrl">
<div>
<button data-ng-click="getSelectedItem(all_titles)">
Select all
</button>
</div>
<div ng-repeat="title in all_titles">
<a ng-click="getSelectedItem(title)">{{title}}</a>
</div>
<hr>
<div>
{{selection}}
</div>
</div>