angular - 如何从对象 url 以角度 ngFor 显示图像?
问题描述
for (let i = 0; i < newsValues.length; i++) {
this.newsX[newsValues[i]] = {
title: newsValues[i].title,
date: newsValues[i].published_on,
url: newsValues[i].url,
body: newsValues[i].body,
image: newsValues[i].imageurl,
tags: newsValues[i].tags
};
}
在这里我得到了响应,我用我需要的属性制作了自己的对象,当我通过它时,除了图像之外,一切正常*ngFor
。
这是我的 HTML 部分:
<nz-layout>
<nz-content>
<header>
<h1>Cool Articles</h1>
</header>
<div class="band">
<div class="item-7" *ngFor="let news of newsData">
<a href="{{news.url}}" class="card">
<div class="thumb" [style.background-image]="'url('+news.image+')'">
</div>
<article>
<h1>{{news.title}}</h1>
<span>Release Date: {{news.date | date: 'yyyy-MM-dd'}}</span>
</article>
</a>
</div>
</div>
</nz-content>
</nz-layout>
解决方案
检查“news.image”值是否具有 http/https 协议。如果不是,您可能必须将其添加到原始数组或模板中。
image: newsValues[i].imageurl, //Check for http/https
确保 'thumb' 类具有附加的高度和宽度属性。
Stackblitz - https://stackblitz.com/edit/angular-ewpahe?file=src/app/app.component.ts
推荐阅读
- windows - 使用 vue.js 下载输入图像
- delphi - IDE 中的文件排序顺序丢失
- windows - 从资源管理器菜单获取文件路径到 Powershell 变量
- git - 使用令牌通过 SSH 克隆 GitLab,无需 SSH 密钥
- android - Firebase 自定义通知声音未播放
- sql - 根据列中的值按列动态排序
- spring - 当 pageSize 较低时,Spring Batch JDBCPagingItemReader 性能下降
- azure - Azure Web 应用程序连接到 azure sql 失败?
- laravel - Laravel 日期到德国月份名称?
- python-3.x - 这是什么意思“first_match = bool(text) and pattern[0] in {text[0], '.'}”?这是python中的正则表达式匹配问题