Daniel Williams Daniel Williams - 4 months ago 12
AngularJS Question

Apply a class with Angular data

I have an item in my array that is

true
or
false
.

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

Answer

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.

Comments