ng-class if object exists angularjs

Anil Sharma picture Anil Sharma · May 15, 2014 · Viewed 9.8k times · Source

How can we activate class if object exists. i have a json array where i need to activate a class if image_id exists for that particular array.

something like ng-if="data.image_id"  then class get activated

 <div class="test" ng-class="classonimage:data.image_id">
   //showing data

<div>  

Here class test is active but classonimage would be activated to modify few css attributes if image_id exists.

So here i want to make some change to story if it contains image. How can we activate ng-class on ng-if. How can we achieve this?

Answer

akn picture akn · May 15, 2014

Try:

<div class="test" ng-class="{'classonimage':data.image_id}"><div>

It's important to place class name between ' ', because if class name contains - ng-class will not work.

Fiddle: http://jsfiddle.net/YwU77/1/