Question

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?

Was it helpful?

Solution

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/

OTHER TIPS

Check if data.image_id is defined or not, and correct your syntax for ngClass:

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

Use ng-class="{classonimage:data.image_id} to control the class of this element.

Here is the example plunk :

You will see the class applies the font-size(in style.css) if data.image_id is true.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top