为什么这个Angular过滤器应用于LIKE ID?

时间:2017-02-13 22:44:00

标签: angularjs

我有一系列3个选项,删除在其他字段中选择的选项。

Plunker

<label>Choice 1</label>
<select ng-model="food.fruitOne"
        ng-options="fruit.id as fruit.name for fruit in fruits | filter:food.fruitTwo!=''?{id: '!' + food.fruitTwo}:{} | filter:food.fruitThree!=''? {id: '!' + food.fruitThree}:{}">
  <option value=""></option>
</select>
<label>Choice 2</label>
<select ng-model="food.fruitTwo"
        ng-options="fruit.id as fruit.name for fruit in fruits | filter: food.fruitOne!=''?{id: '!' + food.fruitOne}:{} | filter: food.fruitThree!=''?{id: '!' + food.fruitThree}:{}">
  <option value=""></option>
</select>
<label>Choice 3</label>
<select ng-model="food.fruitThree"
        ng-options="fruit.id as fruit.name for fruit in fruits | filter: food.fruitOne!=''?{id: '!' + food.fruitOne}:{} | filter: food.fruitTwo!=''?{id: '!' + food.fruitTwo}:{}">
  <option value=""></option>
</select>

除了这个问题外,效果很好;

它似乎像id一样过滤。因此,如果选择1,则具有1x id的项目也会从后续选择中过滤掉。与其他类似的ID相同 - 2&amp; 2次。

显然我不够明确。有什么遗漏?

谢谢!

1 个答案:

答案 0 :(得分:1)

过滤器可以接受在您的情况下可能有用的功能,因为您必须处理3种不同的情况

 $scope.filterFruitsA = function(val) {
    return val.id !== val.id !== $scope.food.fruitTwo && val.id !== $scope.food.fruitThree
  }
  $scope.filterFruitsB = function(val) {
    return val.id !== $scope.food.fruitOne && val.id !== $scope.food.fruitThree
  }
  $scope.filterFruitsC = function(val) {
    return val.id !== $scope.food.fruitOne && val.id !== $scope.food.fruitTwo
  }

链接到工作的plunker https://plnkr.co/edit/gzuhYYX2P1Wd0OtfLgOU?p=preview

我确信它可以巧妙地完成,但是当天我想不出任何事情,明天我会尝试重新审视这个问题