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?

JohnRobertPett
  • 1,133
  • 4
  • 21
  • 36

3 Answers3

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

background-image requires url() to work.

Umur Kontacı
  • 35,403
  • 8
  • 73
  • 96
3

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

'background-image': 'url(https://i.stack.imgur.com/mfvI9.jpg)'

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

Ivan Chernykh
  • 41,617
  • 13
  • 134
  • 146
3

AngularJS now suppoorts ng-style.

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