我得到了以下問題:我需要將schoolList作爲參數傳遞給ngClick函數(以便可以與其他數組重新使用該函數)。但是當我嘗試處理函數內的數組時,使用'group'局部變量,它不會更新數組本身。角度傳遞一個變量作爲函數的參數和處理
如果我在函數中使用'scope.schoolsList'而不是'group',一切正常。
有沒有辦法解決這個問題,並使其工作?我的指令
相關相提並論:
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);
}
}
的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>
組= group.filter()似乎並不正確,你可以嘗試聲明組變量的指令級不是函數作用域。 –