I got the following issue: I need to pass schoolList to the ngClick function as a parameter (to be possible to re-use the function with other arrays). But when I try to deal with the array inside the function, using the 'group' local variable, it does not update the array itself.
if I use 'scope.schoolsList' instead of 'group' in the function, everything works fine.
is there a way to deal with this and make it work?
relevant par of my directive:
link: {
scope.schoolsList = [
'item 1',
'item 2',
'item 3',
'item 4',
];
scope.addItem = function(obj, array, group){
array.push(obj);
group = group.filter(function(list){
return list !== obj;
});
};
scope.removeItem = function($index, group){
group.push(scope.newData.schools[$index]);
scope.newData.schools.splice($index,1);
console.log(scope.newData.schools);
}
}
Relevant par of the html
<select id="newSchool" name="newSchool" ng-model="newSchool" ng-options="school for school in schoolsList | orderBy:school">
<option value="" hidden>-- Select --</option>
</select>
<a ng-click="addItem(newSchool, newData.schools, schoolsList)">add</a>
<ul class="list-inline">
<li ng-show="newData.schools.length > 0">
<ng-pluralize count="newData.schools.length"
when="{'0': '','one': 'School: ','other': 'Schools: '}">
</ng-pluralize>
</li>
<li ng-repeat="school in newData.schools">
<span>{{ school }}</span>
<a ng-click="removeItem($index, schoolsList)">[X]</a>
</li>
</ul>