In the early days of my Angular journey, I have a simple question.
Currently, I am utilizing the WordPress REST API to showcase a list of posts from a specific category by using posts?categories={ID HERE}. However, I am facing an issue in passing the ID from the categories-list to the posts-service.ts file. The component responsible for listing the articles is called category-single.component.ts.
categories-list.component.html
<ul *ngFor="let category of categories">
<li><a (click)="selectCategory(category.id)">{{category.name}}</a>
</li>
</ul>
categories-list.component.ts
selectCategory(id) {
this.router.navigate(['/category', id]);
}
app-routing.module.ts
{
path: 'category/:id',
component: CategorySingleComponent
}
posts.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());
}
}