代码之家  ›  专栏  ›  技术社区  ›  Yes Webs

如何在Angularjs中停止ng选项重复

  •  0
  • Yes Webs  · 技术社区  · 9 年前

    过滤器的Angular JS选择

    我有一个创建搜索模块,它能够使用输入类型文本进行搜索。。现在我想在表单中添加select选项来搜索和过滤它

    这是Plunker: http://embed.plnkr.co/yPJADtUDcgU720dx8ChJ/preview

    2 回复  |  直到 9 年前
        1
  •  1
  •   Huy Hoang Pham    9 年前

    您可以从json中提取类别,然后删除重复项:

    //Filter all the categories, return an array of duplicated categories
    var allCategories = json.modules.map(function(item) {
      return item.category;
    });
    
    //Remove the duplication from the first array
    var filteredCategories = [];
    allCategories.forEach(function(item) {
      if (filteredCategories.indexOf(item) < 0) {
        filteredCategories.push(item);
      }
    });
    
    //Assign the filtered array to scope
    $scope.categories = filteredCategories;
    

    也更改html:

    <select ng-model="search.category" ng-options="category for category in categories"></select>
    

    工作人员: http://plnkr.co/edit/Z6IUMTzePO7UoWoGuUfP?p=preview

        2
  •  0
  •   Tai Huynh    9 年前

    只需使用ng模型=“ 搜索 “像这样:

    <select ng-model="search" ng-options="module.category for module in ocw.modules"></select>
    

    链接演示: http://plnkr.co/edit/U0DudD80VIf0ExKh5ixO?p=preview