【发布时间】:2020-05-30 16:27:20
【问题描述】:
我正在尝试构建 Angular 项目,我有两个不同的 API 新闻源。我想像这样随机显示两个 API 新闻:
- 元素新闻1
- 元素新闻2
- 元素新闻1
- 元素新闻2
但我在下面的代码中拥有的是
- 元素新闻1循环
- 元素新闻2循环
news.ts
news_one_array:any
news_tow_array:any
ngOnInit() {
this.news_one()
this.news_tow()
}
async news_one() {
this.http.get("/nodes/25/iq/0/20/xlarge")
.pipe(timeout(5000),
catchError(err => {
console.log(err)
return Observable.throw("Timeout has occurred");
})).subscribe(data => {
this.news_one_array = data
})
}
async news_tow() {
this.http.get("/nodes/25/iq/0/21/xlarge")
.pipe(timeout(5000),
catchError(err => {
console.log(err)
return Observable.throw("Timeout has occurred");
})).subscribe(data => {
this.news_tow_array = data
})
}
HTML:
// *ngfor news 1
<div *ngFor="let items of news_one_array; let i=index">
<p>{{items.title}}</p>
</div>
// *ngfor news 2
<div *ngFor="let items of news_tow_array; let i=index">
<p>{{items.title}}</p>
</div>
输出:
【问题讨论】:
-
在下面找到我的答案。
标签: arrays angular sorting httpclient shuffle