AngularJS过滤隐藏存在于其他数组中的项
AngularJS Filter Hide Item If It Exists In Another Array
我有两个数组:
$scope.arr1 = [1, 2, 3];
$scope.arr2 = [2, 3, 4];
I'm try to:
- ng-repeat through
arr1
- 使用自定义过滤器仅显示
arr1
中不属于arr2
的项目 - 输出为
1
这是我的观点:
<ul>
<li ng-repeat="item in arr1 | matcher">
{{item}}
</li>
</ul>
这是我的控制器:
var app = angular.module('plunker', []);
app.filter('matcher', function () {
for (var i = 0; i < $scope.arr1.length; i++) {
for (var j = 0; j < $scope.arr2.length; j++) {
if ($scope.arr1[i] != $scope.arr2[j]) {
return $scope.arr1[i];
}
}
}
});
app.controller('MainCtrl', function($scope) {
$scope.arr1 = [1, 2, 3];
$scope.arr2 = [2, 3, 4];
});
这是我的Plunker:http://plnkr.co/edit/Pd3QwMMNfmL62vvdD1kW?p=preview
你知道如何让这个自定义过滤器工作吗?
你永远不会以任何方式访问角过滤器中的$scope
,你应该将它们作为参数传递给过滤器,然后在过滤器函数中访问它们。为了让它更干净,你可以使用.filter
&.indexOf
.