Javascript 背景图像未显示在AngularJS中

Javascript 背景图像未显示在AngularJS中,javascript,html,css,angularjs,background-image,Javascript,Html,Css,Angularjs,Background Image,我的自定义背景图像没有显示在AngularJS中。不知道为什么 当我在控制台中测试$scope.setBackground时,我得到对象{background image:“url(image.jpg)”} 控制器: app.controller('backgroundImageCtrl', function($scope, $http) { $scope.setBackground = { 'background-image' : 'url(image.jpg)'

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

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

控制器:

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


...

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

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

添加学生 {{message}} 查看学生 {{message}}
var-app=angular.module('myApp',[]);
app.controller(“myCtrl”,函数($scope){
$scope.style={};
$scope.setBackground=功能(日期){
$scope.style={
“背景图像”:“url(”https://www.gravatar.com/avatar/10821c595d354140ee66f2a04fb11b7c?s=32&d=identicon&r=PG&f=1")'
}
};
});

背景
看到背景了吗

按照@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。我认为这是因为
背景图像
位于
模板的标签上。它应该在一个内部元素上。你的问题得到回答了吗?@MoshFeu正是这样,我将控制器和ng样式放在div中,现在可以工作了,这是因为你不能设置
script
tag的样式。另外,据我所知,模板引擎获取
脚本
标记的内部内容,并将其放在它应该放在的任何地方。因此,这些属性
ng controller
ng style
不会影响。
<script type = "text/ng-template" id="/page.html">
    <div ng-controller="backgroundImageCtrl" ng-style="setBackground">
    ...
    </div>
</script>