也许有人可以帮助我。我在一个对象中有一个数组。我可以从对象中获取Array(Articles)
。那不是我的问题。我的问题是,我现在在 Angular 中使用 *ngFor
来读取文章列表,但没有 id
。 ID
已生成。
如何让 Angular 读取此内容?
我可以通过放入articles[0]或articles 1来读取数组但我需要获取所有文章,单独指定是行不通的。任何帮助都会很棒!多谢你们。
TS页面
blog: string[];
blogs: string[] = [];
ngOnInit() {
this.getNewsData().subscribe((blog) => {
this.blogs.push(blog);
console.log(blog.articles);
});
component.html 文件
<mat-card class="example-card" *ngFor="let blog of blogs.articles">
<mat-card-header>
<div mat-card-avatar class="example-header-image"></div>
<mat-card-title>{{ blog.title }}</mat-card-title>
<mat-card-subtitle>{{ blog.author }}</mat-card-subtitle>
</mat-card-header>
<img mat-card-image src="{{ blog.urlToImage }}" alt="Photo of a Shiba Inu">
<mat-card-content>
<p>
{{ blog.titles }}
</p>
</mat-card-content>
<mat-card-actions>
<button mat-button>LIKE</button>
<button mat-button>SHARE</button>
</mat-card-actions>
</mat-card>
最佳答案
问题是您将从 getNewsData()
获取的对象推送到数组中,但这并没有多大意义,因为您只关心该对象。
将代码更改为:
blogs;
ngOnInit() {
this.getNewsData().subscribe((blogs) => {
this.blogs = blogs;
});
关于javascript - 在 Angular 中读取对象内的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50372365/