我在首页(homePageComponent)中具有搜索功能,当我单击搜索时,它将重定向到另一个名为搜索列表页面(searchListComponent)的页面,而searchListComponent本身又有一个组件(根据应用程序设计),称为搜索卡组件(searchCardComponent),其中显示所有搜索结果。我试图使用eventemitter将搜索文本从homePageComponent发送到searchCardComponent,但是不知何故数据没有通过。如果有人对此提供帮助,那就太好了。
下面的示例代码
homePageComponent
HTML(产品A)
<ng-select
class="col-md-5 solution-list-dropdown"
[items]="productType$ | async"
[addTag]="true"
[(ngModel)]="selectedSolutionId"
bindLabel="description"
bindValue="id"
placeholder="Select"
[clearable]=true
[searchable]=true
[dropdownPosition]="'down'"
[ngClass]="{ 'dropdown-is-invalid': submitted && f.category.errors }">
</ng-select>
<button class="red-button" (click)="searchRedirection(selectedSolutionId)">
Search
</button>
打字稿
@Output() searchValue = new EventEmitter<string>();
public searchRedirection(selectedSolutionId: string) {
this.searchValue.emit(selectedSolutionId);
this.router.navigate(['/category/' + selectedSolutionId]);
}
SearchListComponent
HTML
<div class="container-fluid product-card-container p-0" id="hor-divider">
<div class="product-list">
<app-product-card (searchValue)="onApplyEvent($event)" *ngFor="let product of products | filter:searchText | filter:filterText" [product]="product">
</app-product-card>
</div>
</div>
Typescript
onApplyEvent(event: any): any {
console.log('event : ' + event);
}
这里我期望控制台日志打印“事件:产品A”,以便我可以利用它将这个值绑定到* ngFor中。任何帮助,将不胜感激。