i'm learning angularjs , have small question:
i have following array returned user
userpreferences = [7,5,4]
i have object i'm using ng-repeat display news , looks this:
{ "id": 1, "preferences": [ 3, 4 ] }
so, want use in ng-repeat, array returned user preferences, , sort news displaying preferences first, below other news, possible?
something like:
<li ng-repeat="new in news | filter:{'userpreferences' : new.preferences}">
i don't know if have use filter, orderby or sort, please?
you can have own function returns number of matched preferences
on scope:
function intersect(array1, array2) { return $filter('filter')(array1, function(n){ return array2.indexof(n) != -1; }); } $scope.matchedpreferences = function(n) { return intersect(n.preferences, $scope.userpreferences).length; };
and pass orderby
filter:
<li ng-repeat="n in news | orderby:matchedpreferences:true">{{n}}</li>
this order news number of matched preferences in descending order.
here fiddle