0
我試着從帖子列表中獲得第一個資產。該列表來自web api。ng對於奇怪的不同行爲
import { Component, OnInit } from '@angular/core';
import { BlogPostService } from '../services/blog-post.service';
import { BlogPost } from '../modules/blog-post';
@Component({
selector: 'af-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
blogPosts: BlogPost[];
skip: number;
showPrevious = false;
constructor(private blogService: BlogPostService) { }
ngOnInit() {
this.skip = 0;
this.getPosts(this.skip);
}
getPosts(skip: number) {
this.skip = skip;
this.blogService.getBlogPosts(skip).subscribe(dt => this.blogPosts = dt});
}
getAssetById(id: string) {
console.log('id:', id);
}
}
奇怪的行爲發生在前端。 blogPosts是4個但僅有2個避風港圖像的列表。
,如果我嘗試只是打印這樣的價值觀:
<div class="col-sm-6" *ngFor="let post of blogPosts">
<div class="card">
{{ post.fields.images && post.fields.images[0].sys.id}}
</div>
</div>
結果是4個格,只是用IDS 2。這是我期待的。
<div class="col-sm-6" *ngFor="let post of blogPosts">
<div class="card">
{{ post.fields.images && getAssetById(post.fields.images[0].sys.id) }}
</div>
</div>
但在這種情況下,控制檯會記錄4次。它記錄圖像存在的帖子,並重復不存在的值。