簡體   English   中英

如何使用 *ngFor 獲取 json 數據 X 次

[英]How to get json data X times using *ngFor

我想從我的端點獲取 3 個圖像,這些圖像有很多使用 angular *ngFor(或任何其他可用方法)的圖像

這是我的 component.html

<div class="col-sm-4 col-xs-6" *ngFor="let result of data.response.constructor(3)">
  <div class="panel-thumbnail"><img class="img-responsive"  width="200" [src]="result.thumbnail"></div>
 </div>

ts文件

export class HomeComponent implements OnInit {
public data:any = []
constructor(private http: HttpClient) {}
getData(){
  const url ='myUrl'
this.http.get(url).subscribe((res)=>{
  this.data = res
  console.log(this.data)
})}
ngOnInit(): void {
 this.getData()}}

我嘗試按照其他帖子的建議使用 [].constructor(3) 但我沒有得到任何結果。 有人可以幫忙嗎

您可以使用 slice 來獲取具有預期項目數的數組

<div class="col-sm-4 col-xs-6" *ngFor="let result of data.response.slice(0,3)">
  <div class="panel-thumbnail"><img class="img-responsive"  width="200" [src]="result.thumbnail"></div>
 </div>

語法是

array.slice(first, numberOfElement)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM