javascript - 从 ng-repeat 中删除重复项

标签 javascript html angularjs angularjs-ng-repeat

<分区>

我有一个非常简单的数组,它有重复的值。我的数组是这样的:

$scope.type= ["Bar", "Pie", "Line", "Line", "Line", "Line", "Line", "Line", "map", "Line", "Bar", "Pie", "Pie", "Pie", "Pie", "Pie", "Pie", "Pie"]

在我的ng-repeat中,我有这个条件

ng-repeat = types in type track by $index

如何在 ng-repeat 中仅显示数组中的唯一值?

最佳答案

您可以在使用ng-repeat 的同时使用unique 过滤器。

ng-repeat="type in types|unique: type"

var app = angular.module('myApp',['ui.directives', 'ui.filters']);
app.controller("myCtrl", function($scope) {
  $scope.types = ["Bar", "Pie", "Line", "Line", "Line", "Line", "Line", "Line", "map", "Line", "Bar", "Pie", "Pie", "Pie", "Pie", "Pie", "Pie", "Pie"];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui/0.4.0/angular-ui.min.js"></script>
<body ng-app="myApp" ng-controller="myCtrl">
  <div ng-repeat="type in types|unique: type">{{type}}</div>
</body>

关于javascript - 从 ng-repeat 中删除重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49247681/

相关文章:

javascript - 获取 $(this) 元素的 html 并插入到 $scope.variable 中

javascript - Backbone.js 教程和学习资源

javascript - 为什么 CoffeeScript 将类定义包装在闭包中?

javascript - Slickgrid - 替换 AJAX 源中的所有网格数据

html - Emmet - 我可以编写 html 以便 css 结构自动生成吗?

javascript - AngularJS ng-class,表单脏时的条件

node.js - 在 typescript 中使用带有 Angular 6 的 Node 短 ID 模块

javascript - 如何在android PhoneGap中加载动态div的iscroll

html - 新网站宽度 >100%

javascript - innerHTML 和