繁体   English   中英

ng-options中相关选择的过滤器

[英]filter of related selects in ng-options

我有两个(三个) select标签,它们列出了相同的选项(麸皮)。 我想防止用户在这些选择中选择相同的值。

控制器:

$scope.brands = [
  {id: '1', name: 'Peugeot'},
  {id: '2', name:'BMW'},
  {id: '3', name:'Mercedes'}
];

在模板中,我尝试通过两种方式使用filterfilter器:

<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函数:

$scope.compareBrands = function(id, already_selected) {
  return id != already_selected;
}

这些都不起作用。 这里是一个plunker我的问题。

谢谢。

可能的方法是使用compareBrands函数作为表达式,将第三个过滤器更改为:

<select ng-model="conf.brand2" 
          ng-options="b.id as b.name for b in brands | filter : compareBrands"></select>

并将compareBrand函数用于:

$scope.compareBrands = function(obj) {
  return obj.id !== $scope.conf.brand1;
}

参见改装的矮人

编辑:我想指出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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM