如何在Angularjs中停止ng-option复制重复

时间:2015-03-23 06:09:34

标签: json angularjs ng-options

Angular JS选择过滤器

我有一个创建搜索模块,可以使用输入类型文本进行搜索..现在我想在表单中添加选择选项以搜索过滤它

以下是Plunker:http://embed.plnkr.co/yPJADtUDcgU720dx8ChJ/preview

2 个答案:

答案 0 :(得分:1)

您可以从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

答案 1 :(得分:0)

只需使用ng-model =“搜索”,就像这样:

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

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