문제

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?

도움이 되었습니까?

해결책

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/

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top