2015-10-06 104 views
1

我似乎无法在使用ng样式设置背景图像后,将CSS样式应用于元素。在angularjs中添加背景图像,css无法正常工作

HTML:

<body id="bg" ng-controller="BgImagesListController" ng-style="selBGImg"> 

CSS:

#bg { 
    background: center center fixed transparent; 
    -webkit-background-size: contain; 
    -moz-background-size: contain; 
    -o-background-size: contain; 
    background-size:contain; 
} 

JS:

var mlwcApp = angular.module('mlwcApp', []) 
.controller('BgImagesListController', function($scope, $http) { 

    $scope.selBGImg = { 
     background : 'url(bgimgs/bg.jpg)' 
    }; 
}); 

我用来设置背景图片在CSS和它的工作。使用ng样式后,图像可以显示,但所有其他样式都消失了。任何想法?

回答

2

变化的背景为 '背景图片': 'URL(bgimgs/bg.jpg)'

var mlwcApp = angular.module('mlwcApp', []) 
 
.controller('BgImagesListController', function($scope, $http) { 
 
    $scope.selBGImg = { 
 
     'background-image' : 'url(https://wiki.teamfortress.com/w/images/thumb/e/ec/Das_Naggenvatcher.png/250px-Das_Naggenvatcher.png)' 
 
    }; 
 

 
})
[id="bg"] { 
 
    background: center center fixed transparent; 
 
    -webkit-background-size: contain; 
 
    -moz-background-size: contain; 
 
    -o-background-size: contain; 
 
    background-size:contain; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<body id="bg" ng-app="mlwcApp" ng-controller="BgImagesListController" ng-style="selBGImg"> 
 
    
 
</body>

+0

它的工作原理!谢谢,但为什么background-image在这里工作? –

+0

,因为你重写了背景属性,在这里你只改变背景图像。 – vivid

+0

我现在明白了,再次感谢您! –