gpt4 book ai didi

javascript - 在 Angular 上从数组中搜索过滤器

转载 作者:行者123 更新时间:2023-12-02 23:00:19 25 4
gpt4 key购买 nike

我有一个存储汽车列表的应用程序。我想将搜索属性添加到我的项目中。我决定为我的搜索框使用管道和 ngModel 来做到这一点。我有一个由订阅监听的汽车数组() 每当它发生任何变化时。在管道中,我有 const obj 变量,它会按照我的意愿根据组件中的 ngModel 过滤数组。但是 Angular 给出错误并显示 ExpressionChangedAfterItHasBeenCheckedError: Expression检查后发生了变化。先前值:“未定义:[object Object],[object Object],[object Object]”。当前值:“未定义:”。。我猜它一开始是未定义的,这就是它发生的原因。但是我添加了 if block 以检查是否未定义,而不是得到此错误。这是我的组件和管道

汽车列表组件.html

  <form class="example" style="margin:auto;max-width:200px">
<input [(ngModel)]="nameBrand" type="text" placeholder="Search.." name="search2">
</form>
<div class="row">
<div class="col-xs-12">
<app-car-item
*ngFor="let carEl of cars | orderName:nameBrand ;let i = index"
[car]="carEl"
[index]="i"
>
</app-car-item>
</div>
</div>

OrderPipe.ts

@Pipe({
name: 'orderName',
})
export class OrderNamePipe implements PipeTransform{
constructor(private carService:CarService)
{

}
transform(value: any, arg1: any)
{
const obj = this.carService.getCars().filter(s => s.brand.includes(arg1));
if(obj)
{
this.carService.setCars(obj);
}
}
}

Car-list.component.ts

  ngOnInit() {
this.subscription=this.carService.carsChanged
.subscribe(
(cars:Car[])=>
{
this.cars=cars;
}
);
this.cars = this.carService.getCars();
}

Car.Service.ts

        export class CarService
{
carsChanged=new Subject<Car[]>();
private cars: Car[]=[
new Car(
'Dodge Viper SRT10',
'Coupe',
2017,
645,
600,
'Gasoline',
'Used',
'http://cdn-ds.com/stock/2010-Dodge-Viper-SRT10-ACR-Ltd-Avail-Akron-OH/seo/ECL2585-1B3AZ6JZ6AV100278/sz_88264/b22eddbbf045c389bd39e4ede1328f13.jpg',
970000
),
new Car(
'Chevrolet Camaro',
'Coupe',
2012,
432,
600,
'Gasoline',
'Used',
'https://carimages.com.au/MdzpzcZ7iNNuMu7RlH3Eg5t30CM=/fit-in/800x540/filters:stretch(FFFFFF)/vehicles/used/2018/CHEVROLET/CAMARO/2018-CHEVROLET-CAMARO-used-78-JWF447-1.jpg',
274000
),
new Car(
'Bentley Continental-GT',
'Coupe',
2018,
601,
489,
'Gasoline',
'New',
'https://cdn1.autoexpress.co.uk/sites/autoexpressuk/files/2017/11/4bentleycontinentalgt.jpg',
4150000
)
]
constructor()
{}
setCars(cars: Car[]) {
this.cars = cars;
this.carsChanged.next(this.cars.slice());
getCars()
{
return this.cars;
}
}

汽车模型.ts

export class Car{
public brand:string;
public type:string;
public year:number;
public bhp:number;
public torque:number;
public fuel:string;
public condition:string;
public imageUrl:string;
public price:number;

constructor(brand:string,type:string,year:number,bhp:number,torque:number,fuel:string,condition:string,imageUrl:string,price:number)
{
this.brand=brand;
this.type=type;
this.year=year;
this.bhp=bhp;
this.torque=torque;
this.fuel=fuel;
this.condition=condition;
this.imageUrl=imageUrl;
this.price=price;
}
}

最佳答案

我认为这不是使用管道内服务的数据的好方法。试试这个管道,

import { Pipe, PipeTransform, Injectable } from '@angular/core';

@Pipe({
name: 'orderName'
})

@Injectable()
export class OrderNamePipe implements PipeTransform {
transform(items: any[], field: string, value: string): any[] {
if (!items) {
return [];
}
if (!field || !value) {
return items;
}
return items.filter(singleItem => singleItem[field].toLowerCase().includes(value.toLowerCase()));
}
}

这样,您的管道就可以重复使用。将您的模板更改为,

<app-car-item
*ngFor="let carEl of cars | orderName : 'brand' : nameBrand ;let i = index"
[car]="carEl"
[index]="i"
>
</app-car-item>

指定了“品牌”,因为您需要根据该字段进行过滤。

关于javascript - 在 Angular 上从数组中搜索过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57817138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com