1

I have html structure

<div class="myDiv">
   <div style="width:200px; height:200px;background-image:url('img/200x200/{{largeImg}}.png');"
      ng-class="{'magictime foolishIn': 1}">
        <span>
            {{largeImg}}
        </span>
   </div>
</div>

using ng-class I want to apply css class to current element which will invoke certain animation on that element like here

css class is correctly applied when I look using firebug but animation is not invoked. When I replace this code with hardcoded values everything works

<div style="width:200px; height:200px;background-image:url('img/200x200/1.png');"
          ng-class="{'magictime foolishIn': 1}">
            <span>1</span>
       </div>

from angularjs I'm using $scope.largeImg as property for binding on the view.

2
  • 2
    img/200x200/{{largeImg%8}}.png What is the %8? Commented Sep 14, 2015 at 10:09
  • it's typo leftover. fixed now. Commented Sep 15, 2015 at 6:11

2 Answers 2

1
.myInnerDiv {
   width: 200px;
   height: 200px;
   height:200px;background-image:url('img/200x200/{{largeImg % 8}}.png');
}

First your content in 'style' property can be a CSS class like above. Also, if you want to trigger animation by adding a CSS class, you can just add it 'class' property like below.

<div class="myDiv">
<div class="myInnerDiv magictime foolishIn">
    <span>
        {{largeImg}}
    </span>

However, if you want to add it based on a flag, which gets updated by clicking a button, you can use 'ng-class' directive. Look at this JsFiddle.

Update: Based on OP's comments. The answer is updated in the JSFiddle

To achieve your functionality, you need to watch out for changes to your target value. I recommend writing a directive for this, since it is what the directives are for - to manipulate UI.

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

5 Comments

thank you for your time but this doesnt work in my code. Whenever $scope.largeImg value is changed css animation doesnt work.
Ahh! So your question is actually incorrect. Your question should be 'How to trigger an animation whenever I change $scope.largeImg value?'
I updated the answer - check out the last example of the JSFiddle
Great PasanRatnayake, just one more question, I need to use dynamic image instead of this 3 hardcoded from your directive example.
Whichever helps you to achieve the functionality you want in your application. Of course, you can move the code in the directive to your controller, then you'd have to duplicate code to make it work. Directives make great re-usable components, if done right. Also, if you think this is the answer, mark is as the answer, so then thread can close and it will help someone who's looking for the answer too
0

i think you can use ng-style

<div style="width:200px; height:200px;" 
     ng-style="{'background-image': 'url(img/200x200/{{ largeImg }}.png)'}"
     ng-class="{'magictime foolishIn': 1}">

1 Comment

thank you for your time but this doesnt work in my code. Whenever $scope.largeImg value is changed css animation doesnt work.

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.