javascript - ng-options inside ng-repeat 删除已经选择的选项值

标签 javascript angularjs

简单的问题,但我不确定如何解决。

我有 ng-repeat,它使用相同值的 ng-options 动态创建选择框

我有 ng-repeat,迭代模型选项。

<div data-ng-repeat="condition in model.conditions">
    <label>{{condition}}</label>
    <select data-ng-model="selectedValue"
            ng-options="item.name for item in optionList">
    </select>
</div>

这是条件模型:

$scope.model = 
    {
        conditions:
        [
            {id:1,name:"CONDITION_ONE"},
            {id:2,name:"CONDITION_TWO"}
        ]
    }

这是选项列表项:

$scope.optionList = 
    [
        {id:1, name:"OPTION_ONE"},
        {id:2, name:"OPTION_TWO"},
        {id:3, name:"OPTION_Three"}
    ];

仅仅是说明性的图像:

enter image description here

我想做的是,当从上面的选择框中选择一个项目时,我想从下面的选择框中删除这个项目,因此,通过删除已经选择的项目来防止重复的项目.

有什么方法可以使用 angularJS 做到这一点吗?

最佳答案

在ng-repeat中使用过滤器

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">
<div data-ng-repeat="condition in model.conditions">
    <label>Remove Id {{condition.id}} from  List </label>
<select ng-model="selectedName" ng-options="item.name for item in filter(condition)">
</select>

</div></div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.optionList = 
    [
        {id:1, name:"OPTION_ONE"},
        {id:2, name:"OPTION_TWO"},
        {id:3, name:"OPTION_Three"}
    ];
    
    $scope.model = 
    {
        videos:
        [
            {id:1,name:"CONDITION_ONE"},
            {id:2,name:"CONDITION_TWO"}
        ],
        conditions : [
        {id:1, name:"OPTION_ONE"},
        {id:2, name:"OPTION_TWO"},
        {id:3, name:"OPTION_Three"}
    ]
    };
    
    $scope.filter = (item)=>{
   		return $scope.optionList.filter(list => list.id !== item.id);
    }
});
</script>

</body>
</html>

关于javascript - ng-options inside ng-repeat 删除已经选择的选项值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54886971/

相关文章:

javascript - jquery 注册表单,如果语句不起作用,则发送电子邮件

javascript - jQuery如何计算实际屏幕距离?

javascript - 创建登录页面的更好方法

angularjs - 警告 : Added non-passive event listener to a scroll-blocking 'touchmove' event when md-select is in md-tabs

javascript - 无法在未启动的 $http.get 请求中读取未定义的属性 'data'

javascript - 将字符串更改为双引号

javascript - 如何在主线程和网络 worker 之间共享 pouchDB

javascript - onClick 处理程序在每个渲染周期触发

javascript - Angular : Is this the correct way to structure a $http POST request?

java - 从 Angular.js 中的 JSON 读取电话号码时出现错误