14

Is it possible / and how / to write inline css style using angular variables?

<div style="background: transparent url({{eventInfo.eventHeaderImg}}) top center no-repeat;"></div> 

This is my output

"NetworkError: 404 Not Found - http://test/eventInfo.eventHeaderImg"

So I see it didn't render this value.

I know it's possible to change this in controller, but is it doable with my approach?

5
  • Doesn't your eventInfo.eventHeaderImg variable in current $scope hold 'eventInfo.eventHeaderImg' value? Commented Apr 25, 2014 at 9:34
  • It works for me: jsfiddle.net/ExpertSystem/zEtT9 Commented Apr 25, 2014 at 9:36
  • yes, when I print {{eventInfo.eventHeaderImg}} in html I get correct value. Commented Apr 25, 2014 at 9:45
  • @Djomla: In the fiddle provided above, I use {{eventInfo.eventHeaderImg}} in the style attribute as an argument to url (the exact same thing you do in your question). And it works fine. I think there was a bug with the style attribute in earlier versions of Angular. What version are you using ? Commented Apr 25, 2014 at 10:17
  • @Djomla: 1.2 what ? Try using the latest version. Commented Apr 26, 2014 at 7:56

4 Answers 4

20

You should use ngStyle:

an example: http://plnkr.co/edit/qT8skZzTwXjrh3Ye5mr9?p=preview

<div ng-style="{ background: 'transparent url({{ eventInfo.eventHeaderImg }}) top center no-repeat' }"></div> 
Sign up to request clarification or add additional context in comments.

5 Comments

Hmm Im not sure why it's not working in my case. I have correct value in variable, but it's no rendering.
ng-style="{background: transparent 'url({{eventInfo.eventHeaderImg}})' top center no-repeat;}" I got this error "Error: [$parse:syntax] Syntax Error: Token ''url(/8330.png?547933175)'' is unexpected, expecting [}] at column 27 of the expression"
try this: ng-style="{background: 'transparent url({{eventInfo.eventHeaderImg}}) top center no-repeat'}" the whole expression should be inside single quotes.
Still same.. Im getting something like this stackoverflow.com/questions/23272145/…
There is not reason to use ngStyle. See my comment under the question. If you are using interpolation ({{}}) it should be working with style. Unless you use an older version of Angular.
5

This worked for my:

<div ng-style="{ 'background': 'transparent url(' + eventInfo.eventHeaderImg + ') top center no-repeat' }"></div>

Comments

0

You're "writing" a wrong url in your style, that's why you've got a 404 (not found) error.

According to this plunker, if you're $scope is well set up, you print the right data in style attribute.

Note that your eventInfo scope variable has to be a map/object.

Comments

-4

You don't need to use double curl brackets there.

1 Comment

Actually if you don't like to use ng-class and without the curly brackets you could use the approach explained here stackoverflow.com/questions/18248437/…

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.