angular - 使用 ngFor 的内联样式背景图像

标签 angular

我正在尝试为 ngFor 循环中的每个 div 设置背景图像。在我循环的 JSON 中,我有一个属性 property.Image包含图像的 url 字符串。 <img src={{flag.properties.Image}}/>有效,但我试图将图像显示为背景图像的尝试无效。在浏览器检查器中,div 背景图像显示为 {{flag.properties.Image}} 而不是读取我试图指向的 url。我究竟做错了什么?

<h3>Top Flags</h3>
<div class="grid grid-pad">
  <div *ngFor="let flag of flags" (click)="gotoDetail(flag)" class="col-1-4">
    <div class="module flag" style="background-image: url({{flag.properties.Image}})">
      <h4>{{flag.id}}</h4>
      <div><img src={{flag.properties.Image}} height="50px" /></div>
      <div>{{flag.properties.Adaptation}}</div>
    </div>
  </div>
</div>

最佳答案

改用

[style.background-image]="'url('+flag.properties.Image+')'"

另见 In RC.1 some styles can't be added using binding syntax

关于angular - 使用 ngFor 的内联样式背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37898672/

相关文章:

c# - 数字组合

angular - 使用@ngrx/store 进入 angular 2 模块

node.js - Azure 中的 Angular 应用程序显示原始 js 文本

angularfire2:破坏性更新( set() )和非破坏性更新( update() )之间的区别

angular - 引用错误 : "google is not defined"

angular - ng build 时从 nginx 获取 404 Not Found

angular - refCount off 的 rxjs shareReplay 在第一个下游订阅者之前不会订阅源

angular - 在属性绑定(bind)上无限期调用的函数

angular - 通过 ngIf 出现后聚焦元素

angular - 从现有项目构建 Angular 元素