Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Css 角度2样式和背景图像_Css_Angular_Angular Directive - Fatal编程技术网

Css 角度2样式和背景图像

Css 角度2样式和背景图像,css,angular,angular-directive,Css,Angular,Angular Directive,我在Angular 2样式指令方面遇到了很大的问题。我无法设置base64编码文件中的背景图像。现在在template.html中,我有以下内容: <div class="projects_item_wrap" [ngStyle]="{'background-image':'url('+images[i].file+')'}"> 其中“images”是base64编码的.png文件及其名称的数组 图像[3]的Console.log.文件给了我这个(问题不在图像中,当我在im

我在Angular 2样式指令方面遇到了很大的问题。我无法设置base64编码文件中的背景图像。现在在template.html中,我有以下内容:

  <div class="projects_item_wrap" [ngStyle]="{'background-image':'url('+images[i].file+')'}">

其中“images”是base64编码的.png文件及其名称的数组

图像[3]的Console.log.文件给了我这个(问题不在图像中,当我在img src='…'中使用它时,它工作得非常好):


有什么想法吗?非常感谢您的回答!:)

收到的base64图像中的换行符是出现问题的原因。 这是我的解决方案:

//This goes to template <div>:
[style.background-image]="makeTrustedImage(images[i].file)"

//And this goes to component:
constructor(private domSanitizer: DomSanitizer) {}

makeTrustedImage(item) {
    const imageString =  JSON.stringify(item).replace(/\\n/g, '');
    const style = 'url(' + imageString + ')';
    return this.domSanitizer.bypassSecurityTrustStyle(style);
  }
//这将转到模板:
[style.background image]=“makeTrustedImage(图像[i]。文件)”
//这就是组件:
构造函数(私有domsanizer:domsanizer){}
makeTrustedImage(项目){
const imageString=JSON.stringify(item).replace(/\\n/g');
常量样式='url('+imageString+');
返回此.domsanizer.bypassSecurityTrustStyle(样式);
}

看看你是不是指domSanitizer-我已经尝试过了-没有任何改变:(可选
[style.background image]=“'url('+images[I].file+')”| SafeSourceURL“
(管道使用
旁路安全信任资源url(值)
请参阅还尝试添加管道-没有结果。还尝试添加了sanitizer.bypassSecurityTrustStyle()-相同,没有结果。对不起,不起作用