html - 如何使用 *ngFor 获取 json 数据 X 次
问题描述
我想从我的端点获取 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)
推荐阅读
- javascript - 如何使用 JavaScript DOM 元素编写更少的代码?
- pg - 使用 Prometheus 的文件系统警报查询
- swift - 我只想通过比较基本上通过webapi从数据库获得的角色名称来移动到另一个屏幕......有什么办法吗?
- firebase - Fcm的MulticastMessage有500条的限制,如果我想一次发送超过500条怎么办?
- c# - 我如何判断一个数字何时完成增加?
- flutter - 颤振错误:“未来”类型的值
'当获取值形式函数Future时,不能分配给'bool'类型的变量 - arrays - 如何创建一个整数数组,大致平均为给定数字?
- python-3.x - 如何在条形图中分隔条形
- javascript - 样式化嵌套的 Shadow DOM
- python - 修复“致命 Python 错误:Py_Initialize:无法初始化 sys 标准流”