Awa Melvine Awa Melvine - 21 days ago 7
AngularJS Question

Displaying a json object on groups using angularjs

I just returned a

users
object in json format from the server. This
users
object is composed of multiple users each having attributes like
first_name
,
last_name
,
phone

email
and
role
.

Now i want to display this object on my html page using angularjs. But I want to group users according to their
role
attribute so that each table on that page will hold a different kind of user (e.g admin, owner etc.)

I already tried this:

<div ng-repeat="user in users">
<div ng-if="user.role == admin">
// list all admins here
</div>
<div ng-if="user.role == owners">
// list all owners here
</div>
</div>


But nothing appears on the page. I am new to angularjs by the way. Thanks for any help.

Answer

Using a filter is maybe a more "angularish" way of doing it.

    <div ng-repeat="user in users | filter:{role:'admin'}">
       {{user.name}}
    </div>

This is a fork of @Sajeetharan's solution.

Comments