简体   繁体   English

Angular 2在[ngStyle]中设置背景图片和颜色

[英]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'}">

Working Plnkr 工作Plnkr

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.

相关问题 如何使用 ngStyle (angular2) 添加背景图像? - How to add background-image using ngStyle (angular2)? 带有ngStyle的AngularJS div背景色 - AngularJS div background color with ngStyle 设置背景图像后设置背景颜色 - Setting background color after setting background image 样式绑定和ngStyle似乎都无法在Angular5的DIV中设置背景图像 - Style binding and ngStyle none seem to work to set background image inside a DIV in Angular5 将背景图像与ngStyle一起使用将返回undefined - Using background-image with ngStyle returns undefined 将div [ngStyle]用于背景图像并对其进行循环* ngFor,如果缺少任何图像,如何使用angular 4在其上显示静态图像 - using div [ngStyle] for background-image and am looping it *ngFor, if any of image is missing ,how to show static image on it using angular 4 使用图像和文本设置画布的背景色 - setting background color of a canvas with image and text 动态背景图像与颜色叠加语法 - 角度 - Dynamic Background image with color overlay syntax - Angular ngStyle Angular2忽略了背景大小的获取 - background-size get's ignored with ngStyle Angular2 React:将双色滑块拇指设置为具有透明背景的图像 - React: Setting a dual color slider thumb to an image with transparent background
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM