2016-09-16 26 views
0

根据一些JSON数据,我得到N个“选择”组件,“选项”由外部API填充。 我需要的是,当一个选项选择在一个“选择”组件中,它不应该显示在其他组件中,我试图应用自定义过滤器,但我还没有运气。Angularjs - 从其他<select>组件中筛选选定的选项

.filter('exclude',function(lodash) { 

     //@param itemsArray: array of items 
     //which fills the options of the select component. 

     //@param selectedItem: the selected <option> 
     //from one of the <select> components. 
     return function(itemsArray, selectedItem) { 
      var output = itemsArray, 
       filteredItemsArray = []; 

      if(selectedItem) { 
       var idx = lodash.findKey(itemsArray, { name: selectedItem.name }); 
       filteredItemsArray.push(selectedItem[idx]); 
       output.splice(idx, 1); 
      } 
      return output; 
     } 
    }); 

这应该是在HTML中的行为:

<select ng-option="p in publi track by $index | exclude: pSelect[$Index])" ng-model="pSelect[$Index]"> 
    <option val="usa" selected>USA</option> 
    <option val="arg">Argentina</option> 
    <option val="col">Colombia</option> 
    <option val="cnd">Canada</option> 
<select> 

<select ng-option="p in publi track by $index | exclude: pSelect[$Index])" ng-model="pSelect[$Index]"> 
    <option val="arg" selected>Argentina</option> 
    <option val="col">Colombia</option> 
    <option val="cnd">Canada</option> 
<select> 

<select ng-option="p in publi track by $index | exclude: pSelect[$Index])" ng-model="pSelect[$Index]"> 
    <option val="col" selected>Colombia</option> 
    <option val="cnd">Canada</option> 
<select> 

我试过,但这个只是从阵列中删除的项目,它从来没有得到选择。

我该怎么办?

+0

请表明了这是如何正在执行... [MCVE] – charlietfl

+0

我预期的行为进行更新。 @charlietfl –

回答

0

如果我理解正确的话,你想筛选出存在于PSELECT除了当前索引

其他 pSelect值,你可以通过传递2个参数做 pSelect和当前指数

然后过滤值

喜欢的东西:

ng-option="p in publi track by $index | exclude: pSelect :$index" 
+0

我试过,但我不能让它工作 –

+0

希望'pSelect'是一个对象是否正确?只要key与当前索引参数不匹配,就可以遍历pSelect的键/值并排除项目。 – charlietfl

+0

是的,它是一个json对象。 –