6

I seem unable to use a controller to set the CSS background property of a div. Here is my code so far, as per the documentation:

HTML:

<div class="hero" ng-controller="HeroCtrl" ng-style="heroImage"></div>

JS:

'use strict';

angular.module('AppliedSiteApp').controller('HeroCtrl', function ($scope) {

    $scope.heroImage = {
        background: 'images/brick.jpg'
    };

    console.log($scope.heroImage);

});

CSS:

.hero {
    width: 100%;
    min-height: 350px;
    background-position: center center;
}

I have also tried to replicate this setup in a Fiddle here. Does anyone have any ideas?

3 Answers 3

8
$scope.heroImage = {
    background: 'url(images/brick.jpg)'
};

background-image requires url() to work.

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

Comments

3

You need to use css' url() function for background images , like this:

'background-image': 'url(https://i.sstatic.net/mfvI9.jpg)'

Plunker: http://plnkr.co/edit/PFUY0w?p=preview

1 Comment

To use with a dynamic data you should do ng-style="{ 'background-image': 'url(' + data.url + ')' }">
3

AngularJS now suppoorts ng-style.

So now you can use ng-style="{ backgroundImage: variableInScope + '/some/string' }"

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.