![](/img/trans.png)
[英]How to filter JSON data in ngfor iteration and get the specified value in <td> in angular 9
[英]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.