1

My custom background image is not showing up in AngularJS. Can't figure out why.

When I test $scope.setBackground in console, I get Object {background-image: "url(image.jpg)"}

Controller:

app.controller('backgroundImageCtrl', function($scope, $http) {
    $scope.setBackground = {
        'background-image' : 'url(image.jpg)'
    };
})

HTML:

<script type = "text/ng-template" id="/page.html" ng-controller="backgroundImageCtrl" ng-style="setBackground">
    <div>
    ...
    </div>
</script>
6
  • you should use setBackground in div that use your template. Commented Apr 11, 2016 at 4:43
  • 1
    I think that it's because the background-image is on the template tag. It's should be on a inner element. Commented Apr 11, 2016 at 4:43
  • is your question answered? Commented Apr 11, 2016 at 12:47
  • @MoshFeu that's exactly it, I put the controller and ng-style into the div and works now Commented Apr 12, 2016 at 19:45
  • 1
    That's because you can't style script tag. Also, as I know, the template engine take the inner content of the script tag and put it wherever it should. So, those attributes ng-controller and ng-style will not affect. Commented Apr 13, 2016 at 7:54

3 Answers 3

1

var app = angular.module('myApp', []);

app.controller("myCtrl", function ($scope) {
    $scope.style= {};
    $scope.setBackground = function (date) {
      $scope.style = {
        'background-image': 'url("https://www.gravatar.com/avatar/10821c595d354140ee66f2a04fb11b7c?s=32&d=identicon&r=PG&f=1")'
      }
    };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>

<div ng-app="myApp">
  <div ng-controller="myCtrl">
    <button ng-click="setBackground()">set background</button>
    <div ng-style="style">see the background</div>
  </div>
</div>

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

Comments

1

Simple solution as suggested by @Mosh Feu, simply place ng-controller="backgroundImageCtrl" and ng-style="setBackground" in an inner div

<script type = "text/ng-template" id="/page.html">
    <div ng-controller="backgroundImageCtrl" ng-style="setBackground">
    ...
    </div>
</script>

Comments

0

you should use setBackground in div that use your template. try like this.

<div ng-include src="/page.html" ng-style="setBackground"></div>

var mainApp = angular.module("mainApp", ['ngRoute']);
mainApp.config(['$routeProvider', function($routeProvider) {
  $routeProvider.

  when('/addStudent', {
    templateUrl: 'addStudent.htm',
    controller: 'AddStudentController'
  }).

  when('/viewStudents', {
    templateUrl: 'viewStudents.htm',
    controller: 'ViewStudentsController'
  }).

  otherwise({
    redirectTo: '/addStudent'
  });
}]);

mainApp.controller('AddStudentController', function($scope) {
  $scope.message = "This page will be used to display add student form";
  $scope.setBackground = {
    'background-image' : 'url(https://www.gravatar.com/avatar/10821c595d354140ee66f2a04fb11b7c?s=32&d=identicon&r=PG&f=1)'
  };
});

mainApp.controller('ViewStudentsController', function($scope) {
  $scope.message = "This page will be used to display all the students";
  $scope.setBackground = {
    'background-image' : 'url(https://www.gravatar.com/avatar/6755dcaf172d19cb9976bedcc56cfed3?s=48&d=identicon&r=PG&f=1)'
  };
});
<script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-route.min.js"></script>
<div ng-app = "mainApp">
  <p><a href = "#addStudent">Add Student</a></p>
  <p><a href = "#viewStudents">View Students</a></p>
  <div ng-view ng-style="setBackground"></div>

  <script type="text/ng-template" id="addStudent.htm">
    <h2> Add Student </h2>
    {{message}}
  </script>

  <script type = "text/ng-template" id="viewStudents.htm">
    <h2> View Students </h2>
    {{message}}
  </script>
</div>

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.