Angular 1.1.5: Empty Fiddle
http://angularjs.org/
by skeemer
HTML
<script src="http://code.angularjs.org/1.2.0rc1/angular.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">Without the comparator you'll see 'a, b, c' but not 'b, a, c' or 'a, c, b' because the tags for 'b then a' are in a different order than filter.tags.
<ul>
<li ng-repeat="l in list | filter:filter">{{ l.name }}</li>
</ul>With the comparator, tags don't need to be in a particular order, but the $ won't match all attributes and other attributes on the expression may not work as expected (and I'd only be able to compair based on type, not key name).
<ul>
<li ng-repeat="l in list | filter:filter:inclusiveComparator">{{ l.name }}</li>
</ul>
</div>
JavaScript
var myApp = angular.module('myApp', []);
//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});
function MyCtrl($scope) {
$scope.filter = {
$: '',
tags: ['a', 'b']
};
$scope.list = [{
name: 'a, b, c',
tags: ['a', 'b', 'c']
}, {
name: 'b, a, c',
tags: ['b', 'a', 'c']
}, {
name: 'a, c, b',
tags: ['a', 'c', 'b']
}, {
name: 'a, c, d',
tags: ['a', 'c', 'd']
}];
$scope.inclusiveComparator = function (expected, actual) {
if (expected && actual) {
console.log(expected, actual);
if (angular.isArray(expected) && angular.isArray(actual)) {
for (var t in actual) {
if (expected.indexOf(actual[t]) == -1) {
return false;
}
}
}
return true;
}
}
}