1

I am having an with using the ng-style directive. I want to create add a button to my ng-repeat which is css sprite. If i include the sprite normal way my element inspector has a good old moan. from the information i gather from the Angular documentation

i thought it was as simple as the following:

<button ng-style="{'background-image':'url:('/img/Myimage.png')'}">test</button>

However i am receiving a snytax error message from this line of code. Does anyone know the correct method?

3
  • 3
    You probably need to escape the quotes in url as: 'url:(\'/img/Myimage.png\')' Commented Nov 24, 2013 at 15:34
  • 1
    syntax for url is incorrect, should not be : after it and quotes on src not required...would remove in angular markup Commented Nov 24, 2013 at 15:54
  • @idbentley thanks i spotted where i have gone wrong Commented Nov 24, 2013 at 16:04

3 Answers 3

1
<button ng-style="{'background-image':'url(\'img/MyImage.png\')'}">test</button> 

is the correct way to do it. was missing the escape the quotes in url 'url:(\'/img/Myimage.png\')'

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

Comments

0

Your CSS syntax is incorrect for url, you have extra : and I would remove quotes for the src so the whole property is one string

<button ng-style="{'background-image':'url(/img/Myimage.png)'}">test</button>

I really don't undertsand why you would use ng-style for this. A simple CSS rule and class would make more sense since you aren't evaluating anything

4 Comments

<button ng-style="{'background-image':'url(\'img/MyImage.png\')'}">test</button> actually work for me
what is point of using ng-styl in the first place for this?
Bcos my button is in an ng-repeat function. if i add this image to my css i get error messages inspector. It's for best practices
that makes no sense at all
0

oh,{color:' something',background:'something '}

ng-style="{background:'url(something)'}"

or

ng-style="{'background-image':'url(something)'}"

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.