html - Angular,如何在一页上显示多个随机报价?
问题描述
我是 Angular (12) 的初学者,我正在努力解决这个问题。我想在页面上显示多个随机报价。我已经设法显示多个,但它们在彼此下方都是相同的。我的代码没有错误。我尝试了一些 forEach 但做不到。这是代码:
app.component.html
<div class="main-content">
<div class="joke-wrapper" *ngFor="let joke of jokes">
<div class="joke">
<p>{{ joke.value }}</p>
</div>
</div>
<div class="joke-wrapper" *ngFor="let joke of jokes">
<div class="joke">
<p>{{ joke.value }}</p>
</div>
</div>
</div>
笑话.service.ts:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root',
})
export class JokesService {
private apiUrl = 'https://api.chucknorris.io/jokes/';
constructor(private http: HttpClient) {}
getRandomJoke() {
return this.http.get(this.apiUrl + 'random');
}
}
app.component.ts:
import { Component, OnInit } from '@angular/core';
import { JokesService } from './jokes.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {
jokes: any[] = [];
constructor(private jokesService: JokesService) {}
ngOnInit() {
this.jokesService.getRandomJoke().subscribe((joke) => {
this.jokes.push(joke);
});
}
}
解决方案
问题是您的数组只包含一个笑话。
这可能有效。
import { Component, OnInit } from '@angular/core';
import { JokesService } from './jokes.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {
jokes: any[] = [];
constructor(private jokesService: JokesService) {}
ngOnInit() {
// Multiple times
this.addJoke();
this.addJoke();
}
addJoke() {
this.jokesService.getRandomJoke().subscribe((joke) => {
this.jokes.push(joke);
});
}
}
虽然我更喜欢这个解决方案:
利用async
管道可以获得一些性能提升,并且它通常是更清洁的代码。
组件 ts:
import { Component, OnInit } from '@angular/core';
import { JokesService } from './jokes.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {
jokes$!: Observable<any[]>;
constructor(private jokesService: JokesService) {}
ngOnInit() {
this.jokes$ = this.jokesService.getRandomJokes(10);
}
}
服务ts:
@Injectable({
providedIn: 'root',
})
export class JokesService {
private apiUrl = 'https://api.chucknorris.io/jokes/';
constructor(private http: HttpClient) {}
getRandomJoke() {
return this.http.get(this.apiUrl + 'random');
}
getRandomJokes(amount: number) {
const jokes = [];
for (let i = 0; i < amount; i++) {
jokes.push(this.getRandomJoke())
}
return forkJoin(jokes);
}
}
组件html:
<div class="main-content">
<div class="joke-wrapper" *ngFor="let joke of jokes$ | async">
<div class="joke">
<p>{{ joke.value }}</p>
</div>
</div>
</div>
推荐阅读
- generics - 用于 Rust 中多个泛型转换的 GADT
- c# - WPF 数据网格 MinWidth=auto MaxWidth=*
- c# - .NET 和 SOAP 标记问题(将对象序列化为 XML)
- reactjs - 如何使用动态路由在页面中触发相同的 .js?
- objective-c-runtime - userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: 未触发
- python - 如何在python中使用列表中的两个条件进行for循环
- c# - 根据另一个组合框值设置组合框项
- webassembly - 将 TSX 与 WebAssembly 一起使用
- .net-core - Bamboo NuGet 还原命令正在运行旧版本的 SDK
- ios - 使用 iOS 和 Swift 访问深度嵌套的 json 数据的最简单方法是什么?