单击“选择黄色”按钮时,我想将黄色添加到所选列表中。黄色被选中,但下拉菜单仍显示黄色。同样,我想在单击“取消选择黄色”按钮时取消选择黄色。我可以取消选择黄色,但黄色没有出现在下拉列表中。请帮我解决这个问题。HTML:
<ui-select multiple ng-model="multipleDemo.colors" theme="select2" ng-disabled="disabled" style="width: 300px;">
<ui-select-match placeholder="Select colors...">{{$item}}</ui-select-match>
<ui-select-choices repeat="color in availableColors | filter:$select.search">
{{color}}
</ui-select-choices>
</ui-select>
<p>Selected: {{multipleDemo.colors}}</p>
<input type="button" value="select yellow color" ng-click="selectYellowColor()"/>
<input type="button" value="deselect yellow color" ng-click="deselectYellowColor()"/>
JS:
$scope.availableColors = ['Red','Green','Blue','Yellow','Magenta','Maroon','Umbra','Turquoise'];
$scope.multipleDemo = {};
$scope.multipleDemo.colors = ['Blue','Red'];
$scope.selectYellowColor = function(){
if($scope.multipleDemo.colors.indexOf($scope.availableColors[3]) == -1){
$scope.multipleDemo.colors.push($scope.availableColors[3]);
}
};
$scope.deselectYellowColor = function(){
if($scope.multipleDemo.colors.indexOf($scope.availableColors[3]) != -1){
var index = $scope.multipleDemo.colors.indexOf($scope.availableColors[3]);
$scope.multipleDemo.colors.splice(index, 1);
}
};
这是 plunker 链接 http://plnkr.co/edit/AHZj1zAdOXIt6gICBMuN?p=preview