Javascript ng选项中相关选项的筛选
我有两(三)个Javascript ng选项中相关选项的筛选,javascript,angularjs,Javascript,Angularjs,我有两(三)个select标签,它们列出了相同的选项(bran)。我想防止用户在这些选择中选择相同的值 控制器: $scope.brands = [ {id: '1', name: 'Peugeot'}, {id: '2', name:'BMW'}, {id: '3', name:'Mercedes'} ]; 在模板中,我尝试以两种方式使用filterfilter: <body ng-controller="FilterCtrl"> <div> <
select
标签,它们列出了相同的选项(bran)。我想防止用户在这些选择中选择相同的值
控制器:
$scope.brands = [
{id: '1', name: 'Peugeot'},
{id: '2', name:'BMW'},
{id: '3', name:'Mercedes'}
];
在模板中,我尝试以两种方式使用filter
filter:
<body ng-controller="FilterCtrl">
<div>
<select ng-model="conf.brand1" ng-options="b.id as b.name for b in brands"></select>
</div>
<div>
<select ng-model="conf.brand2"
ng-options="b.id as b.name for b in brands | filter : !conf.brand1 : true"></select>
</div>
<div>
<select ng-model="conf.brand2"
ng-options="b.id as b.name for b in brands | filter : conf.brand1 : compareBrands"></select>
</div>
</body>
所有这些都不起作用。下面是我的问题的一个例子
谢谢。可能的方法是使用
compareBrands
函数作为表达式,为此,请将第三个过滤器更改为:
<select ng-model="conf.brand2"
ng-options="b.id as b.name for b in brands | filter : compareBrands"></select>
看
编辑:我想指出,conf.brand1
的值将是它的id,因此如果您仍然同意,并且不想为每个select
创建比较器函数,您应该使用此表单:
<select ng-model="conf.brand2"
ng-options="b.id as b.name for b in brands | filter : {id: (conf.brand1 ? ('!' + conf.brand1) : '')}"></select>
$scope.compareBrands = function(obj) {
return obj.id !== $scope.conf.brand1;
}
<select ng-model="conf.brand2"
ng-options="b.id as b.name for b in brands | filter : {id: (conf.brand1 ? ('!' + conf.brand1) : '')}"></select>