基本上我有一个链接,当它被点击时,我显示一个模态.现在我可以在模式上显示其他属性,如标题,除了背景图像!呃!
这是模式:
<div class="modalContainer" ng-style="{'background-image':'url({{selectedMeal.url}})'}">
<div id="modalHeader">
<div style="padding-top: 10px;">{{selectedMeal.title}}</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
这些是链接:
<div ng-click='selectMeal(meal)' class="contentItem" ng-repeat='meal in recipes | filter:searchText' ng-style="{'background-image':'url({{ meal.url }})'}">
<span id="contentItemHeader">{{ meal.title }}</span>
<span id="contentItemLevel">{{ meal.level }}</span>
</div>
Run Code Online (Sandbox Code Playgroud)
JSON:
recipes:[
{
"type": "Breakfast",
"title": "Chili con carne",
"description": "A spicy and fragrant chili with ground beef, kidney beans, tomatoes, onions and garlic. Best served over rice with a dollop of sour cream and some cheese on top.",
"ratings": 4,
"duration": 12,
"level":"medium",
"url":"http://31.media.tumblr.com/bc0ea7c5f95701bff499f78b59d23e68/tumblr_mr74z9Lt3O1rs0z5go1_500.jpg",
"ingredients":
[
{
"vegetable": "40ml"
}
],
"method":
[
{
"1": "In a medium sized stock pot, heat the oil over heat. Saute onions, chile peppers andgarlic until soft."
}
]
},
{
"type": "Breakfast",
"title": "Spicy Noodle",
"description": "A spicy and fragrant chili with ground beef, kidney beans, tomatoes, onions and garlic. Best served over rice with a dollop of sour cream and some cheese on top.",
"ratings": 5,
"duration": 30,
"level":"hot",
"url":"http://38.media.tumblr.com/875b5eeb5b1efa37d2e9d36fbad836d3/tumblr_mzczesVrZD1rimr6yo1_1280.jpg",
"ingredients":
[
{
"vegetable": "40ml"
}
],
"method":
[
{
"1": "In a sized stock pot, heat the oil over heat. Saute onions, chile peppers andgarlic until soft."
}
]
}]
Run Code Online (Sandbox Code Playgroud)
adi*_*rof 80
你使用单引号犯了一些错误,你必须把你的变量放在单引号之外.
对于这个div
<div class="modalContainer" ng-style="{'background-image':'url({{selectedMeal.url}})'}">
Run Code Online (Sandbox Code Playgroud)
这部分被视为一个字符串
'url({{selectedMeal.url}})'
Run Code Online (Sandbox Code Playgroud)
而您希望angular来解析此变量
{{selectedMeal.url}}
Run Code Online (Sandbox Code Playgroud)
所以要解决这个问题,正确的语法是
<div class="modalContainer"
ng-style="{'background-image': 'url(' + selectedMeal.url + ')'}">
Run Code Online (Sandbox Code Playgroud)
Foo*_*ife 10
背景图像的正确语法是:
background-image: url("src");
Run Code Online (Sandbox Code Playgroud)
ng-style的正确语法是:
<div ng-style="{'background-image':'url({{re.url}})'}" ></div>
Run Code Online (Sandbox Code Playgroud)
例如 :
<div ng-repeat="re in recipes">
<div ng-style="{'background-image':'url({{re.url}})'}" style="height: 100px"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
JSFiddle:http://jsfiddle.net/U3pVM/7194/
还 可以使用自定义的指令:
app.directive('backgroundImageDirective', function () {
return function (scope, element, attrs) {
element.css({
'background-image': 'url(' + attrs.backgroundImageDirective + ')',
'background-repeat': 'no-repeat',
});
};
});
Run Code Online (Sandbox Code Playgroud)
例如 :
<div ng-repeat="re in recipes">
<div background-image-directive="{{re.url}}" style="height: 100px"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
JSFiddle:http://jsfiddle.net/U3pVM/7193/
更新:
<div ng-style="'{{re.url}}' != '' && {'background-image':'url({{re.url}})'}" style="height: 100px"></div>
Run Code Online (Sandbox Code Playgroud)
它不会尝试获取不存在的图像.
这对我有用.如果您从json或任何其他人检索数据,请尝试此操作
<div class="your-class" [style.background-image]="'url(' + url.image + ')'" [ngStyle]="{ 'background-size': 'cover','background-repeat': 'no-repeat'} ">
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
80375 次 |
| 最近记录: |