背景图像不是';使用ngStyle在Angular6中工作

背景图像不是';使用ngStyle在Angular6中工作,angular6,ng-style,Angular6,Ng Style,我想使用ngStyle动态设置背景图像。但它没有加载任何内容。这是我的HTML <div id="banner-cover" class="bg-parallax animated fadeIn" [ngStyle]="{ 'background-image': backgroundImage }" > ... </div> 您需要调用getBackgroundImage()方法: [ngStyle]=”{ “背景图像”:getBackgroun

我想使用ngStyle动态设置背景图像。但它没有加载任何内容。这是我的HTML

<div
  id="banner-cover"
  class="bg-parallax animated fadeIn"
  [ngStyle]="{
    'background-image': backgroundImage
  }"
>
...
</div>

您需要调用getBackgroundImage()方法:

[ngStyle]=”{
“背景图像”:getBackgroundImage()

}“

我尝试调用方法和属性。他们两个都不工作。这里我展示了属性调用。
export class BannerComponent implements OnInit {
  public _backgroundImage = "'url('../../../../assets/images/bg-banner.jpg')'";
  constructor(private sanitizer:DomSanitizer) { }

  ngOnInit() {
  }
  get backgroundImage() {
    return this.sanitizer.bypassSecurityTrustStyle( this._backgroundImage );
  }
}