Search code examples
javascriptjsonangularjsif-statementng-class

AngularJS: Find specific item in list of items / if statement in ng-class


I want to highlight the current user in a list of all users. My code looks like this.

<div class='user' ng-repeat='user in users'>
  <span class='name'>{{ user.displayName }}</span>
</div>

I have currentUser.id that I want to compare to user.id as I loop them through and perhaps add a class when the two match. I would like to do something like this in theory:

ng-class='highlight: if user.id == currentUser.id'

Solution

  • This should do it:

    ng-class='{highlight: user == currentUser}'