1

I'm trying to change the background-color of a div based on the value of an Angular expression. I have the following div:

<div class="outer">
  <h4 class="weather-heading">It's currently {{weather}}</h4>
</div>

Based on the value of {{weather}} (i.e. sunny, cloudy), I want to change the background-color of the outer div. What's the best way to do this with angular?

4
  • Have you taken a look at ngClass? Commented Sep 15, 2013 at 16:47
  • the best way is to put the view' logic in directive Commented Sep 15, 2013 at 16:48
  • @Cherniv can you give a code example? Commented Sep 15, 2013 at 16:52
  • you can start from ngClass as incutonez suggested Commented Sep 15, 2013 at 16:55

1 Answer 1

3

As @incutonez said, you could use ng-class:

<div ng-class="{'sunny': weather == 'Sunny', 'rainy': weather == 'Rainy'}">
  <h4 class="weather-heading">It's currently {{weather}}</h4>
</div>

Plunker here.

Depending on what you're trying to accomplish, it may be more appropriate to create a directive, as @Cherniv has suggested.

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

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.