【问题标题】:How to stop ng-option duplication repeat in Angularjs如何在Angularjs中停止ng-option重复
【发布时间】:2015-03-23 16:07:15
【问题描述】:

Angular JS 选择过滤器

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

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

【问题讨论】:

  • 您的选择框 ng 选项数据错误尝试将 {{search.category}} 放入您的 html 并查看该输出。

标签: json angularjs ng-options


【解决方案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

【讨论】:

  • 如何将相同的类别列表项填充到复选框选项.. 这样我一次可以选择多个
  • 复选框有点复杂,我没有干净的解决方案。您可以尝试问其他问题以获得每个人的答案。
【解决方案2】:

只需像这样使用 ng-model="search" :

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    相关资源
    最近更新 更多