4

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?

3 Answers 3

3

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/

Sign up to request clarification or add additional context in comments.

Comments

2

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>

4 Comments

!== throws an error in the console, at least it used to. It does not seem to like triple equal to either. You have to use != and == (had to, not sure if it was fixed)
@callmekatootie It doesn't anymore, for a long time now!
@Blackhole It will not work if class name is like class-on-image.
@czwek Seems obvious, but it's worth mentioning it.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.