Angular's filter provider already has the ability to filter a list (like in ng-repeat) by property. In the HTML, this would be like the following:
<ul ng-repeat="category in categories | filter:{parent: 0}">
------------------------------ HERE --^
DEMO: http://plnkr.co/edit/HXvkmar7I7HpgPbv6LVM?p=preview
Unfortunately, the default matching used is partial. That means, if the parent property contains "0". So for example, any of the items that have parent: 10 would match as well, as the plunker shows (string comparisons are done on the property values).
Luckily, you can provide true in the filter expression and it will do an exact match. That means using this:
<ul ng-repeat="category in categories | filter:{parent: 0}:true">
-------------------------------------------------- HERE --^
DEMO: http://plnkr.co/edit/m22ML9XPf2FnZweuModU?p=preview
Also, just as a suggestion, it seems weird to put an ng-repeat on a <ul> element - I would expect it to be on the <li> element.
Looking at the comments in the other answers, you were also looking to only display unique categories. Since your JSON implies that categories have unique ids, you can create a custom filter for your app like this:
app.filter("uniqueCategoryFilter", function () {
return function (categories) {
var uniqueCategories, catIds;
uniqueCategories = [];
catIds = {};
if (categories) {
angular.forEach(categories, function (category) {
if (!catIds[category.id]) {
catIds[category.id] = true;
uniqueCategories.push(category);
}
});
}
return uniqueCategories;
};
});
And use it like this:
<ul ng-repeat="category in categories | filter:{parent: 0}:true | uniqueCategoryFilter">
-------------------------------------------------------- HERE --^
DEMO: http://plnkr.co/edit/3NHuTvQiVHQTZxtIkEGB?p=preview
Make sure this unique filter comes after the parent property filter.
References:
filtercan filter by property values pretty easilyfilter:{ parent:0 }will not work, due to filter converting it to a String. Which means, as soon as you have a property like this:"parent": 10you gonna have a bad time. Otherwise I would wholeheartedly agree with you :)<li>{{category.parent}}</li>.Filtering by property works, however it filters by string, not by number - I'm no Angular pro, so there might be a way to get around this. If so, this would of course be the most elegant solution to the problem at hand.