2

Hi I have the following code to set the background to the whole page, and it works fine the background image is showed as expected, but the chrome console show this errors

GET http://localhost:3000/assets/img/backgrounds/%7B%7Bbgimg%7D%7D 404 (Not Found)
GET http://localhost:3000/assets/img/backgrounds/ 404 (Not Found)

This error is similar when loading image with src instead of ng-src but for background I don't know any directive. here is my code

HTML:

<body background="assets/img/backgrounds/{{bgimg}}">
<div class="view-container layer">
    <div ng-view></div>
</div>

The controller

    var bgImagesList = ['01.jpg',
        '02.jpg', '03.jpg', '04.jpg',
        '05.jpg', '06.jpg', '07.jpg',
        '08.jpg', '09.jpg', '10.jpg',
        '11.jpg'];

    var changeBaclground = function () {
        $rootScope.bgimg = bgImagesList[Math.floor(Math.random() * bgImagesList.length)];
    };
1
  • 1
    You could consider using ng-class or ng-style I believe. Commented Sep 4, 2015 at 14:48

3 Answers 3

5

You can use ng-style for this purpose

<body ng-style="{'background-image':'url(assets/img/backgrounds/{{bgimg}})'}" > 

Other easy way will be
html:

<body ng-style="setBackground()" >

In Controller:

var bgimg = bgImagesList[Math.floor(Math.random() * bgImagesList.length)];   

$rootScope.setBackground = function(){
    return {
            'background-image':'url(assets/img/backgrounds/' + bgimg + ')'
        }
} 
Sign up to request clarification or add additional context in comments.

5 Comments

I tried this <body ng-style="{'background-image':'assets/img/backgrounds/{{bgimg}})'}"> and this <body ng-style="{'background-image':'assets/img/backgrounds/bgimg)'}"> but didn't work . I am using it correctly?
please see my code, you need to enclose path in url()
Ups sorry, but didn't work, now the resultan code is <body ng-style="{'background-image':'url(assets/img/backgrounds/07.jpg)'}" style="background-image: url(http://localhost:3000/assets/img/backgrounds/);"> and the background is missing
@efirvida i have edited my answer and added another way. Try that.
that last works perfectly, thanks!, I edited to have the full code
2

I'd possibly consider using ng-style for a solution.

Inside your controller when you change the background, update a style object in your $scope which is reference by ng-style of the element.

See some info about ng-style here.

UPDATE

See the following plunkr http://plnkr.co/edit/dYIQ... that shows a simple use of ng-style. When you check and uncheck the checkbox the background color will change between red and blue.

For your problem, your myStyle object could be something like;

var myStyle = {
  background: 'url(path/to/' + bgimg + ')'
}

Ideally, you won't need to create the myStyle variable each time but the example gives you a simple example of how it can be used.

Hope that helps you out!

4 Comments

I saw the documentation but didn't figure out on who to do it
I've updated my answer with a plunkr to show you what I mean. Simply check the checkbox and see the background color change between red and blue.
In later updates I believe you could just do changes like myStyle.background = 'url(...)'; instead of creating the object again. This is just a simple way to show you how it works.
What does your $rootScope.myStyle object look like? have you followed how I used it in the plunker?
1

Use ng-style with your element, this is how to set it:

ng-style="{'background-image':'url(assets/img/backgrounds/{{bgimg}})'}"

And this is how your whole code should be:

<body ng-style="{'background-image':'url(assets/img/backgrounds/{{bgimg}})'}">
<div class="view-container layer">
    <div ng-view></div>
</div>

You can read more about it at ngStyle - directive in module ng and you will find how to use it here.

2 Comments

as I said in other comment with this I have a resultant HTML like <body ng-style="{'background-image':'url(assets/img/backgrounds/07.jpg)'}" style="background-image: url(http://localhost:3000/assets/img/backgrounds/);"> and now the background is missing because style="background-image: url(http://localhost:3000/assets/img/backgrounds/);" don't have the image url
Where are your html file and image located, are they in the same folde? please show us the directory tree. I think you may use ../assets/img/backgrounds/{{bgimg}} if the assets folder and your current html file are in the same directory.

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.