问题描述
现在,我有一个背景图像URL硬编码到CSS中.我想使用AngularJS中的逻辑动态选择背景图片.这是我目前拥有的:
Right now I have a background image URL hard-coded into CSS. I'd like to dynamically choose a background image using logic in AngularJS. Here is what I currently have:
HTML
<div class="offer-detail-image-div"><div>
CSS
.offer-detail-image-div {
position: relative;
display: block;
overflow: hidden;
max-width: 800px;
min-height: 450px;
min-width: 700px;
margin-right: auto;
margin-left: auto;
padding-right: 25px;
padding-left: 25px;
-webkit-box-flex: 1;
-webkit-flex: 1;
-ms-flex: 1;
flex: 1;
border-radius: 5px;
background-image: url('/assets/images/118k2d049mjbql83.jpg');
background-position: 0px 0px;
background-size: cover;
background-repeat: no-repeat;
}
如您所见,CSS中的背景图像引用了特定的文件位置.我希望能够以编程方式确定图像URL的位置.我真的不知道从哪里开始.我不知道JQuery.谢谢.
As you can see, the background image in the CSS references a specific file location. I want to be able to programmatically determine the location of the image URL. I really don't know where to begin. I do not know JQuery. Thank you.
推荐答案
您可以使用ng-style使用AngularJS动态更改CSS类属性.
You can use ng-style to dynamically change a CSS class property using AngularJS.
希望这个ng样式的示例至少可以帮助您理解概念.
Hope this ng-style example will help you to understand the concept at least.
有关 ngStyle
var myApp = angular.module('myApp', []);
myApp.controller("myAppCtrl", ["$scope", function($scope) {
$scope.colors = ['#C1D786', '#BF3978', '#15A0C6', '#9A2BC3'];
$scope.style = function(value) {
return { "background-color": value };
}
}]);
ul{
list-style-type: none;
color: #fff;
}
li{
padding: 10px;
text-align: center;
}
.original{
background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
<div ng-controller="myAppCtrl">
<div class="container">
<div class="row">
<div class="span12">
<ul>
<li ng-repeat="color in colors">
<h4 class="original" ng-style="style(color)"> {{ color }}</h4>
</li>
</ul>
</div>
</div>
</div>
</div>
</body>
编辑1
您可以通过以下方式更改背景图片:URL.
You can change the background-image: URL by following way.
$scope.style = function(value) {
return { 'background-image': 'url(' + value+')' };
}
这篇关于如何在AngularJS中动态更改CSS属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!