I have two arrays:
$scope.arr1 = [1, 2, 3];
$scope.arr2 = [2, 3, 4];
I'm trying to:
- ng-repeat through
arr1
- Use a custom filter to only show items from
arr1
that aren't inarr2
- The output should be
1
Here's my view:
<ul>
<li ng-repeat="item in arr1 | matcher">
{{item}}
</li>
</ul>
Here's my controller:
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];
});
Here's my Plunker: http://plnkr.co/edit/Pd3QwMMNfmL62vvdD1kW?p=preview
Any idea how to get this custom filter working?