Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ng选项中相关选项的筛选_Javascript_Angularjs - Fatal编程技术网

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>