[英]Angular 2 setting background image and color in [ngStyle]
I am working on an Angular 2 project and trying to set both a background image and a opaque gradient using [ngStyle]
These will eventually be variables pulled from JSON, or else I would just write them in my SCSS file. 我正在Angular 2项目上工作,尝试使用
[ngStyle]
设置背景图像和不透明渐变,这些最终将是从JSON提取的变量,否则我会将它们写在SCSS文件中。 This is the code I am trying to use but it's not working. 这是我尝试使用的代码,但无法正常工作。 I can set either a background color or image using
[ngStyle]
, but not both. 我可以使用
[ngStyle]
设置背景色或图像,但不能同时设置两者。
<header [ngStyle]="{
'background': 'linear-gradient(0deg, rgba(119, 233, 207, 0.5),
rgba(119, 233, 207, 0.5)),
url('example.com/image.png') center center / cover no-repeat'
}">
This code in my SCSS file gives me exactly the result I want. 我的SCSS文件中的这段代码为我提供了我想要的结果。
header {
background: linear-gradient(0deg, rgba(119, 233, 207, 0.5),
rgba(119, 233, 207, 0.5)),
url('example.com/img.png');
}
Does anyone know if it is possible to specify both a background image and a opaque gradient using [ngStyle]
? 有谁知道是否可以使用
[ngStyle]
同时指定背景图片和不透明渐变? Thanks! 谢谢!
The problem that you're having stems from the usage of single-quotes ( '
) in the url()
property. 您遇到的问题源于
url()
属性中单引号( '
)的使用。 (Honestly, I'm surprised it doesn't throw an error.) (老实说,我很惊讶它没有抛出错误。)
Removing them displays your desired result: 删除它们将显示您想要的结果:
For Example: 例如:
<header [ngStyle]="{
'background': 'linear-gradient(0deg, rgba(119, 233, 207, 0.5),
rgba(119, 233, 207, 0.5)),
url(example.com/img.png) center center / cover no-repeat'}">
Unfortunately, the standard escape ( \\'
) does not seem to work in this context, but removing the single-quotes entirely is valid as well (assuming the value used for url()
is URL-Encoded). 不幸的是,标准的转义(
\\'
)在这种情况下似乎不起作用,但是完全删除单引号也是有效的(假定用于url()
的值是URL编码的)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.