对我来说,早期的 Angular 日子,希望是简单的问题。
我正在使用 WordPress REST API 并尝试使用posts?categories={ID HERE}显示类别中的帖子列表,我的问题是我不确定如何将 ID 从类别列表传递到帖子- service.ts 将列出文章的组件称为 category-single.component.ts。
类别列表.component.html
<ul *ngFor="let category of categories">
<li><a (click)="selectCategory(category.id)">{{category.name}}</a>
</li>
</ul>
类别列表.component.ts
selectCategory(id) {
this.router.navigate(['/category', id]);
}
应用程序路由.module.ts
{
path: 'category/:id',
component: CategorySingleComponent
}
post.service.ts
export class PostsService {
private _wpBase = "http://base.local/wp-json/wp/v2/";
constructor(private http: Http) {}
getPostList(): Observable < Post[] > {
return this.http
.get(this._wpBase + `posts?categories={ID GOES HERE}`)
.map((res: Response) => res.json());
}
}