我的要求是有多個選擇框具有相同的下拉列表(ng-options)數組,但是如果我在一個選擇框中選擇一個值,則應從其他選擇框下拉列表中刪除它。選擇框的角度不可變陣列
在on change事件中,我試圖創建一個新的數組,但不包括爲第一個數組選擇的項目。但它會從兩個選擇框中刪除。就像一個不可變的DS總是返回一個原始的變異克隆。
上需要的建議是有可能實現這種方式還是有辦法解決它
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.items = [];
$scope.selectedItem = { name: 'two', id: 27 };
$scope.items = [{name: 'one', id: 30 },{ name: 'two', id: 27 },{ name: 'threex', id: 50 }];
$scope.remove=function(data){
$scope.items=$scope.items.filter(item=>{
return item.name!==data.name;
});
});
<!-- begin snippet: js hide: false console: true babel: false -->
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script data-require="[email protected]" data-semver="1.3.15" src="https://code.angularjs.org/1.3.15/angular.js"></script>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<select ng-model="selectedItem" ng-change="remove(selectedItem)" ng-options="item.name for item in items track by item.id"></select>
<select ng-model="selectedItem1" ng-options="item.name for item in items track by item.id"></select>
</body>
</html>
$scope.remove=function(data){
$scope.items=$scope.items.filter(item=>{
return item.name!==data.name;
});