代码之家  ›  专栏  ›  技术社区  ›  AlbertMunichMar

找不到不同的支持对象NgStyle backgroundImage

  •  -1
  • AlbertMunichMar  · 技术社区  · 6 年前

    我想在div中显示一个有角度的图像

    <div 
      ngStyle="{'backgroundImage': 'url({{getImage()}})'}"
    >
      <div class="text-wrapper">
        text
      </div>
    </div>
    

    以及我正在接通的照片的地址:

      ngOnInit() {
        this.fullImagePath = 'assets/hackaton_home.png';
      }
    
      getImage() {
        return this.fullImagePath;
      }
    

    图像位于文件夹资源中。

    enter image description here

    有什么帮助吗?

    4 回复  |  直到 6 年前
        1
  •  4
  •   dev-assassin    6 年前
    <div [ngStyle]="{ 'background-image': 'url(' + fullImagePath+ ')'}"></div>
    
        2
  •  1
  •   René Winkler    6 年前
     [ngStyle]="{'backgroundImage': 'url({{getImage()}})'}"
    
        3
  •  0
  •   Sachila Ranawaka    6 年前

    为什么不只添加路径变量

    <div 
      [ngStyle]="{'background-image': 'url(' + fullImagePath + ')'}"
    >
    

    <div 
      [ngStyle]="{'background-image': 'url(' + getImage() + ')'}"
    >
    
        4
  •  0
  •   Sajeetharan    6 年前

    您应该将返回的url传递给变量,按如下所示进行更改,

     [ngStyle]="{'background-image':'url('+ getImage()')'}"