繁体   English   中英

AngularJS中未显示背景图像

[英]Background image not showing up in AngularJS

我的自定义背景图片未在AngularJS中显示。 不知道为什么。

当我在控制台中测试$scope.setBackground时,我得到了对象{background-image: "url(image.jpg)"}

控制器:

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>

 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> 

@Mosh Feu建议的简单解决方案,只需将ng-controller="backgroundImageCtrl"ng-style="setBackground"放在内部div中

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

您应该在使用模板的div中使用setBackground。 尝试这样。

<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> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM