【发布时间】:2019-03-02 13:59:41
【问题描述】:
我已经完成了英雄之旅教程(两次)和 Brad Traversy 的 Angular-Front-to-Back。我有一点(函数式)Python 经验,但仍在尝试围绕 Angular 语法和类的工作原理展开思考。
作为实践,我正在制作一个基于 ToH 的图书馆网络应用程序,我可以在其中使用单独的服务将书籍和作者存储在单独的组件中,以通过 HTTP 服务获取它们。
按照Angular Style Guide 关于将逻辑放在组件中而不是在模板中,我不成功制作了一个组合的书/作者视图组件,显示作者和他们分别写了哪些书,反之亦然。 (我不想把它放在 HTML 模板中。)
这里是 author.component.ts。 book.component.ts 是相同的,只是作者被替换为“book/books”:
import { Component, OnInit } from '@angular/core';
import { Author } from '../../models/author';
import { AuthorService } from '../../services/author.service';
@Component({
selector: 'app-authors',
templateUrl: './authors.component.html',
styleUrls: ['./authors.component.css']
})
export class AuthorsComponent implements OnInit {
authors: Author[];
constructor(private authorService: AuthorService) { }
ngOnInit() {
this.getAuthors();
}
getAuthors(): void {
this.authorService.getAuthors().subscribe(authors =>
this.authors = authors);
}
Book/AuthorService 类似于 Tour of Heroes 的“HeroService”,因为它们使用 http.get:
/** GET authors from the server */
getAuthors (): Observable<Author[]> {
return this.http.get<Author[]>(this.authorsUrl);
模型 author.ts 和 book.ts
export class Author {
id: number;
firstName: string;
lastName: string;
booksAuthored?: number[];
}
export class Book {
id: number;
authorId?: number; // TODO: allow multiple authors
title: string;
}
我可能已经正确理解了 observable 是一个流对象(而不是 JSON 类型的对象),不能像普通数组一样被切片。
我想要一个可以连接 author.lastName 和 author.firstName 的函数,然后列出属于各自作者的书籍。我已经能够使用 ngFor(让作者的作者)和 ngIf(如果 book.authorId === author.id)在 HTML 模板中做到这一点,但现在我想在组件(或服务? )
【问题讨论】:
-
其中一个答案解决了您的问题吗?
-
是的,谢谢,我认为您的回答最接近我的需要。然而,K Adrian 的回答让我对这个库 webapp 实验的其他部分有了一些了解。我对两者都投了赞成票,但我的声望只有 6 个,所以它没有显示。 :-)
标签: angular