英文:
What causes the failure to use an Angular custom pipe in a component?
问题
I have been working on an e-commerce app in Angular 14.
我一直在开发一个基于 Angular 14 的电子商务应用。
I have a list of products that I want to filter by brand and, for this purpose, I have made the following custom pipe (app/pipes/filter-products.pipe.ts
):
我有一个产品列表,我想要按品牌进行筛选,为此,我创建了以下自定义管道(app/pipes/filter-products.pipe.ts
):
import { Pipe, PipeTransform } from '@angular/core';
import { Product } from '../models/product';
@Pipe({
name: 'filterProductsByBrand',
})
export class FilterProductsPipe implements PipeTransform {
transform(input: Product[], brand?: String): any {
let output: Product[] = [];
if (brand == '') output = input;
else output = input.filter((item) => item.brand == brand);
return output;
}
}
I have imported the pipe in app.modules.ts
and added it to the declarations
array.
我已经在 app.modules.ts
中导入了这个管道,并将它添加到了 declarations
数组中。
In the products-list.component.ts
file I have:
在 products-list.component.ts
文件中,我有以下内容:
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Product, ProductResponse } from '../../models/product';
import { ProductService } from '../../services/product.service';
@Component({
selector: 'app-products-list',
templateUrl: './products-list.component.html',
styleUrls: ['./products-list.component.css'],
})
export class ProductsListComponent implements OnInit {
selectedBrand: String = '';
productResponse!: ProductResponse;
products: Product[];
constructor(private ProductService: ProductService, private Router: Router) {}
ngOnInit(): void {
this.getProducts();
}
public getProducts() {
this.ProductService.getProducts().subscribe((response) => {
this.productResponse = response;
this.products = this.productResponse.products;
});
}
}
In the above component's template:
在上面组件的模板中:
<div class="mb-3">
<select
[(ngModel)]="selectedBrand"
(change)="getProducts()"
class="form-select"
>
<option selected value="">All brands</option>
<option value="Apple">Apple</option>
<option value="Samsung">Samsung</option>
</select>
</div>
<!-- Card list Begin -->
<ng-container *ngIf="productResponse">
<div class="row">
<div
*ngFor="let product of products | filterProductsByBrand: selectedBrand"
class="product col-sm-6 col-md-4"
>
<app-product-item [product]="product"></app-product-item>
</div>
</div>
</ng-container>
<!-- Card list End -->
The problem
问题是:
The code fails to compile and I get the error:
代码无法编译,我得到以下错误:
Property 'filterProductsByBrand' does not exist on type 'ProductsListComponent'.
在 'ProductsListComponent' 类型上不存在属性 'filterProductsByBrand'。
Stackblitz
Stackblitz示例链接: https://stackblitz.com/edit/angular-ivy-r1yymn?file=src/app/components/products-list/products-list.component.ts
Questions
问题:
-
What am I doing wrong?
- 我做错了什么?
-
What is the most reliable way to fix this issue?
- 修复这个问题的最可靠方法是什么?
英文:
I have been working on an e-commerce app in Angular 14.
I have a list of products that I want to filter by brand and, for this purpose, I have mede the following custom pipe (app/pipes/filter-products.pipe.ts
):
import { Pipe, PipeTransform } from '@angular/core';
import { Product } from '../models/product';
@Pipe({
name: 'filterProductsByBrand',
})
export class FilterProductsPipe implements PipeTransform {
transform(input: Product[], brand?: String): any {
let output: Product[] = [];
if (brand == '') output = input;
else output = input.filter((item) => item.brand == brand);
return output;
}
}
I have imported the pipe in app.modules.ts
and added it to the declarations
arary.
In the products-list.component.ts
file I have:
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Product, ProductResponse } from '../../models/product';
import { ProductService } from '../../services/product.service';
@Component({
selector: 'app-products-list',
templateUrl: './products-list.component.html',
styleUrls: ['./products-list.component.css'],
})
export class ProductsListComponent implements OnInit {
selectedBrand: String = '';
productResponse!: ProductResponse;
products: Product[];
constructor(private ProductService: ProductService, private Router: Router) {}
ngOnInit(): void {
this.getPoducts();
}
public getPoducts() {
this.ProductService.getProducts().subscribe((response) => {
this.productResponse = response;
this.products = this.productResponse.products;
});
}
}
In the above component's template:
<div class="mb-3">
<select
[(ngModel)]="selectedBrand"
(change)="getPoducts()"
class="form-select"
>
>
<option selected value="">All brands</option>
<option value="Apple">Apple</option>
<option value="Samsung">Samsung</option>
</select>
</div>
<!-- Card list Begin -->
<ng-container *ngIf="productResponse">
<div class="row">
<div
*ngFor="let product of products | filter: filterProductsByBrand"
class="product col-sm-6 col-md-4"
>
<app-product-item [product]="product"></app-product-item>
</div>
</div> </ng-container
><!-- Card list End -->
The problem
The code fails to compile and I get the error:
> Property 'filterProductsByBrand' does not exist on type 'ProductsListComponent'.
Stackblitz
I have put toghteter this stackblitz.
Questions
- What am I doing wrong?
- What is the most reliable way to fix this issue?
答案1
得分: 3
尝试使用<div *ngFor="let product of products | filterProductsByBrand: 'brand-name'">
英文:
<div *ngFor="let product of products | filter: filterProductsByBrand">
The selector for your pipe is filterProductsByBrand
but here you are trying to use a pipe called filter
and passing the string "filterProductsByBrand"
as a second argument.
Try <div *ngFor="let product of products | filterProductsByBrand: 'brand-name'">
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论