【发布时间】:2020-07-19 20:44:21
【问题描述】:
我在主页(homePageComponent)中有搜索功能,当我点击搜索时,它将重定向到名为搜索列表页面(searchListComponent)的不同页面,并且我在 searchListComponent 本身(根据应用程序设计)中还有一个组件,称为搜索卡组件(searchCardComponent),我在其中显示所有搜索结果。我试图使用 eventemitter 将搜索文本从 homePageComponent 发送到 searchCardComponent 但不知何故数据没有通过。如果有人能帮忙就好了。
下面的示例代码
主页组件
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]);
}
搜索列表组件
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>
打字稿
onApplyEvent(event: any): any {
console.log('event : ' + event);
}
在这里,我希望控制台日志打印“事件:产品 A”,以便我可以利用它将这个值绑定到 *ngFor。任何帮助将不胜感激。
【问题讨论】:
-
请为您的项目创建一个 stackblitz 并分享它
标签: javascript angular typescript angular-ui-router angular-directive