Daniel Williams Daniel Williams - 2 months ago 5x
AngularJS Question

Apply a class with Angular data

I have an item in my array that is


I use this to say

<div ng-show="{{ item.isDirectory }}">

I also want to use the same rule to apply a class to that div if
item.isDirectory == true


First, it's incorrect:

<div ng-show="{{ item.isDirectory }}">

ngShow waits an expression, it works just as below:

<div ng-show="item.isDirectory">

Also, preferably use ngIf directive as a good practice.

To apply a class in your div based on a condition, you should use ngClass directive:

<div ng-class="{ 'yourTrueclass': item.isDirectory, 'yourFalseClass': !item.isDirectory }"></div>

Take a look on this tutorial.