-2
我已阅读了大部分与此类似的问题,但仍然不明白如何在我的应用中使.find()
工作。Angular 4 - 如何正确使用.find()?
我有API服务:
export class VehicleService {
private defUrl = 'API';
constructor(private http: Http) { }
getVehicleByVehicleId(vehicleid?: any) {
const url = (!vehicleid) ? this.defUrl : 'API1&vehicle_id=' + vehicleid;
return this.http.get(url)
.map(res => res.json());
}
searchVehicleId(description?: string) {
const url = (!description) ? this.defUrl : 'API2&description=' + description;
return this.http.get(url)
.map(res => res.json().vehicles);
}
}
组件
export class VehicleComponent implements OnInit {
ngOnInit() {
this.searchQuery = new FormGroup({
vehicleDescription: new FormControl('', Validators.required)
});
}
constructor(private vehicleService: VehicleService) { }
public fleet: Fleet[];
public description: Description[];
public searchQuery: FormGroup;
public searchVehicleId: any;
public searchByVehicleId(searchQuery) {
this.vehicleService
.searchVehicleId(searchQuery.value.vehicleDescription)
.subscribe(searchQuery => {
this.description = searchQuery;
this.searchVehicleId = this.description.find() // <= HOW TO IMPLEMENT THIS?
this.vehicleService
.getVehicleByVehicleId(this.searchVehicleId)
.subscribe(searchQuery => {
this.vehicle = searchQuery;
})
});
}
interface Vehicle {
status: number;
dallases: Vehicle[];
}
interface VehicleDetails {
vehicle_id: number;
dallassettings: string;
dallasupdated: string;
dallas_list: DallasList[];
}
interface DallasList {
number: number;
auth: number;
}
//////////////////////////////////
//////////////////////////////////
interface Description {
vehicles: DescriptionDetails[];
}
interface DescriptionDetails {
id: number;
custom_description: string;
description: string;
}
API2
{
"vehicles": [{
"description": "SKODA ROOMSTER",
"id": 123456,
"custom_description": ""
}, {
"description": "SKODA ROOMSTER",
"id": 123456789,
"custom_description": ""
}]
}
所以,我在这里做的是:通过description
内FormGroup
- vehicleDescription
字段至searchByVehicleId
方法。在这里我想找到id
通过vehicleDescription
velue(它是API2中的"description"
字段)。当我们找到id
时,我们用它进一步在this.vehicleService.getVehicleByVehicleId
...
主要问题是 - 我不知道,如何正确实施.find()
。
由于
https://stackoverflow.com/a/35398031/4826457 –
解决:'this.searchVehicleId = this.description.find(X => X。 id === x.id).id.toString();'。感谢您的帮助,如果我之前发现了这个问题,我的身边就没有问题了。 – Haseoh