I'm working on project which has on the left side of the screen categories, and by pressing on each categoriy in the middle of screen should be displayed articles/products which belong to selected category.
I fectched categories on the left very simple like this:
category: Category[];
ngOnInit() {
this.category= this._activatedRoute.snapshot.data['category'];
}
And in my template .html file I just displayed them and attached event on each:
<div class="tab-content categories-tab-content">
<div class="tab-pane" id="food">
<ul>
<li *ngFor="let c of category;">
<button type="button" data-toggle="" data-target="" class="btn categories-btn" (click)="getArticlesByCategory(c.id)">
{{subgroup.title | uppercase}}
</button>
</li>
</ul>
</div>
</div>
As you can see getArticlesByCategory
method is located there like this: (click)="getArticlesByCategory(c.id)
So when users clicks on each categorie this method is called:
getArticlesByCategory(selectedCategoryId: string) {
this._articleService.getArticlesByCategory(selectedCategoryId).subscribe(articles => {
this.category= category;
});
And articles are fetched, I've tried console.log
and they are there..
Here is my second component whicih should display that articles:
import { Component, OnInit, Input } from '@angular/core';
import { Article } from '../../models/article';
@Component({
selector: 'app-main-article',
templateUrl: './article.component.html',
styleUrls: ['./article.component.css']
})
export class ArticleComponent implements OnInit {
// Here I've created `@Input` because I think it needs to receive this list of articles
@Input() category: Article[];
constructor() { }
ngOnInit() {
}
}
This is template file of my component which should receive and display articles:
<div *ngFor="let product of products ;" class="article-holder">
<div id="article1" class="article">
<p class="article-price">product.price</p>
<p class="article-title">product.title</p>
</div>
</div>
As you can see in my template file code is ready to loop that list, but I don't know how to get it, could anyone please explain me how to do this?
Thanks Cheers