Here's a plunker of what might work for you:
https://plnkr.co/edit/S47fBh2xdT1gp2zrznk4?p=preview
Instead of transferring data from the parent to the child component, both the parent and child component will use the service to receive data.
Parent component:
import { Component, OnInit } from '@angular/core';
import { Product } from './product'
import { ProductsService } from './products.service';
@Component({
selector: 'app-appliances-product-card',
template: `
<div class="container-fluid">
<button (click)="filterProduct('Type 1')">Filter Type 1</button>
<button (click)="filterProduct('')">Clear Filter</button>
<div class="col-sm-9">
<app-products-card></app-products-card>
</div>
</div>
`
})
export class AppliancesProductCardComponent implements OnInit {
products: Product[];
constructor(private _productsService: ProductsService){ }
filterProduct(type: string) {
this.products = this._productsService.filterProduct(type);
}
ngOnInit() {
this.products = this._productsService.getAllProducts();
}
}
Children's component:
import { Component, OnInit, Input } from '@angular/core';
import { Product } from './product';
import { ProductsService } from './products.service';
@Component({
selector: 'app-products-card',
template: `
<h1>Product Card</h1>
<div *ngFor="let product of products">{{product.name}} - {{product.type}}</div>
`
})
export class ProductsCardComponent implements OnInit {
constructor(private _productsService: ProductsService){ }
ngOnInit() {
this.products = this._productsService.getAllProducts();
this._productsService.filteredProducts.subscribe(products => {
console.log(products);
this.products = products
});
}
}
Services:
import { EventEmitter, Injectable } from '@angular/core';
import { Product } from './product';
export class ProductsService {
filteredProducts = new EventEmitter<Product[]>;
private products: Product[] = [
{ id: 1, name: 'Product 1', price: 10.50, type: 'Type 1' },
{ id: 2, name: 'Product 2', price: 15.50, type: 'Type 1' },
{ id: 3, name: 'Product 3', price: 1.50, type: 'Type 2' },
{ id: 4, name: 'Product 4', price: 100.50, type: 'Type 3' }
];
getAllProducts(): Product[] {
return this.products.slice();
}
filterProduct(type: string): Product[]{
let filteredProducts = this.products.filter(p => !type || p.type == type).slice();
this.filteredProducts.emit(filteredProducts);
}
}
, angular. . , .
( angular) , .
- rxjs. , angular .
ngrx/store ( Angular). .
, , , .