2

So say I have a JSON object 'user' with some basic properties like: 'name', 'address', 'role', etc.

I want the ng-repeat to only spit out the objects in which the property 'role' equates to 'administrator'. How would I go about doing that?

I'm thinking something like a custom filter, or possibly making a scope variable where I put the matching objects in an array, and then repeat through them.

What is the best practice for this case?

Thanks.

1 Answer 1

10

The built in filter can handle this case...

<div ng-repeat="user in users | filter : {role: 'administrator'} : true">
   ...
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

Quick question: this works fine for top-level properties, but my user case requires me to filter by a property that is one level down. $scope.user = User.get(); $scope.user.role.name; is the property I need to filter by.
It'd take slightly more work but you should be able to pass a predicate function into the filter to apply any custom logic you need. For example: function (value) { return value.role.name === "administrator"; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.