如何使用 ng-style 设置 div 的背景图像 [英] How to Set Background image of div with ng-style
本文介绍了如何使用 ng-style 设置 div 的背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
基本上我有一个链接,当它被点击时,我会显示一个模式.现在我可以在模态上显示其他属性,如标题,除了背景图像!呃呃!
这是模态:
<div id="modalHeader"><div style="padding-top: 10px;">{{selectedMeal.title}}</div>
这些是链接:
<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>
json:
食谱:[{"type": "早餐","title": "辣椒酱","description": "一种香辣香辣的辣椒,搭配碎牛肉、芸豆、西红柿、洋葱和大蒜.最好搭配米饭配上一团酸奶油和一些奶酪.",评分":4,持续时间":12,"level":"中等","url":"http://31.media.tumblr.com/bc0ea7c5f95701bff499f78b59d23e68/tumblr_mr74z9Lt3O1rs0z5go1_500.jpg",配料":[{蔬菜":40ml"}],方法":[{1":在一个中等大小的汤锅中,将油加热.炒洋葱、辣椒和大蒜直至变软."}]},{"type": "早餐","title": "辣面","description": "一种香辣香辣的辣椒,搭配碎牛肉、芸豆、西红柿、洋葱和大蒜.最好搭配米饭配上一团酸奶油和一些奶酪.",评分":5,持续时间":30,"level":"热","url":"http://38.media.tumblr.com/875b5eeb5b1efa37d2e9d36fbad836d3/tumblr_mzczesVrZD1rimr6yo1_1280.jpg",配料":[{蔬菜":40ml"}],方法":[{1":在一个大小合适的汤锅中,将油加热.炒洋葱、辣椒和大蒜直至变软."}]}]
解决方案
您在使用单引号时犯了一些错误,您必须将变量放在单引号之外.
对于这个 div
这部分被视为字符串
'url({{selectedMeal.url}})'
而您希望 angular 解析此变量
{{selectedMeal.url}}
所以要解决这个问题,正确的语法是
Basically i have a link, and when it's clicked, i display a modal. Now i can display other properties on the modal like title except the background Image ! urghhh !
This is the modal:
<div class="modalContainer" ng-style="{'background-image':'url({{selectedMeal.url}})'}">
<div id="modalHeader">
<div style="padding-top: 10px;">{{selectedMeal.title}}</div>
</div>
</div>
These are the links:
<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>
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."
}
]
}]
解决方案 You have made some mistakes using single quotes, you have to take your variable outside the single quotes.
For this div
<div class="modalContainer" ng-style="{'background-image':'url({{selectedMeal.url}})'}">
This part is being treated as a string
'url({{selectedMeal.url}})'
Whereas you would want angular to parse this variable
{{selectedMeal.url}}
So to solve this, the correct syntax is
<div class="modalContainer"
ng-style="{'background-image': 'url(' + selectedMeal.url + ')'}">
这篇关于如何使用 ng-style 设置 div 的背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文