【发布时间】:2022-01-23 17:22:11
【问题描述】:
我是 Stackoverflow 和 Angular 的新手。我正在用 Angular 编写一个前端应用程序(电子商务)。我将单击事件绑定到 mat-menu 的按钮,以便具有以下行为:单击按钮时,我想显示已单击类别的产品。例如,如果我单击“彩妆”按钮,我希望显示所有类别为“彩妆”的产品。问题是当我点击按钮时,浏览器上什么都没有显示,但我认为应用程序的逻辑是有效的,因为如果我打开浏览器的控制台,就会打印出所选类别的产品。
product.component.html:
<mat-menu #menu="matMenu">
<button mat-menu-item (click)="categoryFilter('Make-up')"> Make-up</button>
...
</mat-menu>
product.component.ts:
@Component({
selector: 'app-product',
templateUrl: './product.component.html',
styleUrls: ['./product.component.css'] })
export class ProductComponent implements OnInit {
public products: Product[] = [];
public product!: Product;
constructor(private productService: ProductService) { }
ngOnInit() {
this.getProducts();
}
public getProducts(): void{
this.productService.getProducts().subscribe(
(response: Product[]) => {
this.products= response;
},
(error: HttpErrorResponse) => {
alert(error.message);
}
);
}
public categoryFilter(category: string): void{
this.productService.getProductsByCategory(category).subscribe(
(response: void) => {
console.log(response);
},
(error. HttpErrorResponse)=>{
alert(error.message);
}
)
} }
【问题讨论】:
标签: html angular angular-material frontend mouseclick-event