0

I just recently started learning Angular and I have a question. I want to implement a search method to search for a product on my site, I made search.pipe.ts, which works, but the input for input is in the header.component.ts component, and the products array is in the car-list.component.ts component.

car-list.component.html

<div *ngFor="let car of cars | paginate: { itemsPerPage: pageNumber, currentPage: currentPg} | **search:searchStr**" class="col-md-3">

  <div class="product box">

    <img src="{{'data:image/jpg;base64,' + car.image }}" alt="">
    <h3>{{ car.name }}</h3>
    <div class="price">{{ car.price | currency:'USD' }}</div>
    <button class="btn btn-primary btn-sm">Add to cart</button> <!--(click)="addToCart(tempProduct)"-->
  </div>
  <br>
</div>

header.component.html

<form class="d-flex me-5">
    <input type="text" class="form-control me-2" placeholder="Search cars...">
</form>

header.component.ts

export class HeaderComponent implements OnInit {
  searchStr: string = '';

  constructor() {
  }

  ngOnInit(): void {
  }

}

search.pipe.ts

@Pipe({
  name: 'search'
})
export class SearchPipe implements PipeTransform {

  transform(cars: any[], value: any) {
    return cars.filter(car => {
      return car.name.includes(value);
    })
  }
}

I want the input values ​​from the header component to be passed to the car-list component so that I can find the product I need.

Ferin Patel
  • 3,424
  • 2
  • 17
  • 49

2 Answers2

0

In this case you can use a shared service where you can pass data from your header component and load that data in your products component. For further reference - Angular 4 pass data between 2 not related components

0

use @Input and @Output decorators to communicate between components