如何使用ngStyle(angular2)添加背景图像?

Ign*_*nat 90 html javascript css angular

如何使用ngStyle添加背景图像?我的代码不起作用:

this.photo = 'http://dl27.fotosklad.org.ua/20121020/6d0d7b1596285466e8bb06114a88c903.jpg';

<div [ngStyle]="{'background-image': url(' + photo + ')}"></div>
Run Code Online (Sandbox Code Playgroud)

Thi*_*ier 198

我想你可以试试这个:

<div [ngStyle]="{'background-image': 'url(' + photo + ')'}"></div>
Run Code Online (Sandbox Code Playgroud)

从阅读你的ngStyle表达,我猜你错过了一些"'"......

  • 请记住,图片URL(图片名称)中的空格可能会导致静默的`[ngStyle]`和`[style.background-image]`渲染失败. (3认同)
  • 我更喜欢`this.photo = \`url($ {photo})\`;``[style.background-image] ='photo'`。 (2认同)

Tod*_*dmy 80

你也可以尝试这个:

[style.background-image]="'url(' + photo + ')'"

  • @ redfox05我想ngStyle是一种语法糖.主要区别在于ngStyle允许您应用多个css规则,但[style.<css_prop>]只允许一个.接下来是等价的:`<div [ngStyle] ="{color:'red','font-size':'22px'}"> First </ div>`和`<div [style.color] ="' red'"[style.font-size.px] ="22">第二个</ div>` (3认同)

Gün*_*uer 23

import {BrowserModule, DomSanitizer} from '@angular/platform-browser'

  constructor(private sanitizer:DomSanitizer) {
    this.name = 'Angular!'
    this.backgroundImg = sanitizer.bypassSecurityTrustStyle('url(http://www.freephotos.se/images/photos_medium/white-flower-4.jpg)');
  }
Run Code Online (Sandbox Code Playgroud)
<div [style.background-image]="backgroundImg"></div>
Run Code Online (Sandbox Code Playgroud)

也可以看看


Uli*_*lko 6

看起来你的风格已被消毒,绕过它尝试使用DomSanitizer的bypassSecurityTrustStyle方法.

import { Component, OnInit, Input } from '@angular/core';
import { DomSanitizer, SafeStyle } from '@angular/platform-browser';

@Component({
  selector: 'my-component',
  templateUrl: './my-component.component.html',
  styleUrls: ['./my-component.component.scss']
})

export class MyComponent implements OnInit {

  public backgroundImg: SafeStyle;
  @Input() myObject: any;

  constructor(private sanitizer: DomSanitizer) {}

  ngOnInit() {
     this.backgroundImg = this.sanitizer.bypassSecurityTrustStyle('url(' + this.myObject.ImageUrl + ')');
  }

}
Run Code Online (Sandbox Code Playgroud)
<div *ngIf="backgroundImg.length > 0" [style.background-image]="backgroundImg"></div>
Run Code Online (Sandbox Code Playgroud)


Vij*_*han 6

改用

[ngStyle]="{'background-image':' url(' + instagram?.image + ')'}"
Run Code Online (Sandbox Code Playgroud)


Tom*_*yon 6

我的背景图像无法正常工作,因为 URL 中有空格,因此我需要对其进行 URL 编码。

您可以尝试使用不包含需要转义字符的其他图像 URL,检查这是否是您遇到的问题。

您可以仅使用内置于encodeURI()方法中的Javascript 对组件中的数据执行此操作。

就我个人而言,我想为其创建一个管道,以便可以在模板中使用它。

为此,您可以创建一个非常简单的管道。例如:

src/app/pipes/encode-uri.pipe.ts

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'encodeUri'
})
export class EncodeUriPipe implements PipeTransform {

  transform(value: any, args?: any): any {
    return encodeURI(value);
  }
}
Run Code Online (Sandbox Code Playgroud)

src/app/app.module.ts

import { EncodeUriPipe } from './pipes/encode-uri.pipe';
...

@NgModule({
  imports: [
    BrowserModule,
    AppRoutingModule
    ...
  ],
  exports: [
    ...
  ],
 declarations: [
    AppComponent,
    EncodeUriPipe
 ],
 bootstrap: [ AppComponent ]
})

export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

src/app/app.component.ts

import {Component} from '@angular/core';

@Component({
  // tslint:disable-next-line
  selector: 'body',
  template: '<router-outlet></router-outlet>'
})
export class AppComponent {
  myUrlVariable: string;
  constructor() {
    this.myUrlVariable = 'http://myimagewith space init.com';
  }
}
Run Code Online (Sandbox Code Playgroud)

src/app/app.component.html

<div [style.background-image]="'url(' + (myUrlVariable | encodeUri) + ')'" ></div>
Run Code Online (Sandbox Code Playgroud)