http在最佳实践中呼叫
我有两个型号;
export interface Student {
id: string,
name: string
}
export interface StudentGoal{
id: string,
studentId: string,
goal: string
}
我让我们的学生服务的学生这样:
getStudents(): Observable<Student[]> {
return this.http.get<Student[]>("URL_1");
}
我得到了这样的学生目标:
getStudentGoal(studendId: string): Observable<StudentGoal> {
return this.http.get<StudenGoal>("URL_2" + studentId);
}
问题
最佳实践是什么,最佳实践在我的学生中获得 *ngfor的学生目标。 。成分
.ts
private subs = new SubSink()
public students: Student[] = [];
constructor(
private studentService: StudentService
)
ngOnInit() {
this.getStudents();
}
ngOnDestroy() {
this.subs.unsubscribe()
}
getStudents(){
this.subs.sink = this.studentService.getStudents().subscribe((res: Student[]) => {
this.students = res
})
}
getStudentGoal(studentId: number){
BEST PRACTICE ?
}
.html
<div *ngFor="let student of students">
{{getStudentGoal(student.id).goal}}
</div>
I have two models;
export interface Student {
id: string,
name: string
}
export interface StudentGoal{
id: string,
studentId: string,
goal: string
}
I get the students in my StudentService like this:
getStudents(): Observable<Student[]> {
return this.http.get<Student[]>("URL_1");
}
I get a student goal like this:
getStudentGoal(studendId: string): Observable<StudentGoal> {
return this.http.get<StudenGoal>("URL_2" + studentId);
}
QUESTION
What is the best practice to get student goal inside *ngFor in my student.goal.component
.ts
private subs = new SubSink()
public students: Student[] = [];
constructor(
private studentService: StudentService
)
ngOnInit() {
this.getStudents();
}
ngOnDestroy() {
this.subs.unsubscribe()
}
getStudents(){
this.subs.sink = this.studentService.getStudents().subscribe((res: Student[]) => {
this.students = res
})
}
getStudentGoal(studentId: number){
BEST PRACTICE ?
}
.html
<div *ngFor="let student of students">
{{getStudentGoal(student.id).goal}}
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1.-当我们掌控可观察到时,我们有两个不同的东西事物:“响应”和“订阅”
this.subscription是一种订阅,只有在我们想要取消订阅时才util。这不是响应
2.-当我们在.html可观察物中使用时,我们可以
使用async pipe
但不是 {{sisscription}}
3.-当我们掌握ngfor订阅时,有时会读取我们所需的一切。为此,我们使用switchmap和forkjoin,
以便我们可以使用
1.- When we mannage Observables we have two differents things: the "response" and the "subscription"
this.subscription is a subscription and only is util when we want unsubscribe. it's NOT the response
2.-When we use in .html observables we can
show a variable that we are using inside subscribe function
use async pipe
but not {{subscription}}
3.-When we mannage a ngFor subscription sometimes is util read all we need. For this we use switchmap and forkJoin
So we can use