类型 'Observable<{ LOC_NA: any; }>' 缺少类型 'Location[]' 的以下属性:length、pop、push、concat 以及另外 25 个
Type 'Observable<{ LOC_NA: any; }>' is missing the following properties from type 'Location[]': length, pop, push, concat, and 25 more
我正在尝试使用 angular material 自动完成功能在值更改时填充下拉列表,但我不断收到以下错误。
Type 'Observable<{ LOC_NA: any; }>' is missing the following properties from type
'Location[]': length, pop, push, concat, and 25 more.
下拉菜单应该填充来自 json 文件的数据。我使用以下文档作为参考 https://material.angular.io/components/autocomplete/examples
请找到下面的代码:
HTML
<input type="text" placeholder={{constant.RENTALLOCATION}} matInput
formControlName="filteredRentalLocation" [matAutocomplete]="auto"
[ngModel]="commonService.selectedRentalLocation">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of filteredLocationOptions | async" [value]="option">
{{option.CCRG_CORP_CD}} {{option.LOC_NA}}
</mat-option>
</mat-autocomplete>
TS
export interface Location {
CCRG_CORP_CD: any;
LOC_MNEM_CD: any;
CITY_NA: any;
LOC_NA: any;
}
export class CreateReservationHomeComponent implements OnInit {
filteredLocationOptions: Observable<Location[]>;
ngOnInit() {
this.initCreateResForm();
//this.createresService.fetchLocationDetails().pipe(map((res:any) => res.json()))
this.filteredLocationOptions =
this.createResForm.get('filteredRentalLocation')!.valueChanges
.pipe(
startWith(''),
map(value => this._filterGroup(value)))
}
private _filterGroup(value: string): Location[]{
if(value){
return this.createresService.fetchLocationDetails()
.pipe(map(locs => ({LOC_NA: locs.LOC_NA }))) <--- //error here
}
}
服务技术支持
locationDetailsURL = url + 'assets/Locations/aulocs.json';
fetchLocationDetails(): Observable<Location[]>{
return this.http.get<Location[]>(this.locationDetailsURL, {withCredentials: true})
JSON数据
[
{
"LOC_MNEM_CD":"XYZ",
"CCRG_CORP_CD":"B",
"CITY_NA": "sddfs",
"LOC_NA": "werwer",
},
{
"LOC_MNEM_CD":"MCD",
"CCRG_CORP_CD":"A",
"CITY_NA": "werwer",
"LOC_NA": "wwew",
}
]
我明白了。我需要过滤 locs 数组。
private _filterGroup(value: string): Observable<Location[]>{
return this.createresService.fetchLocationDetails()
.pipe(map(locs => locs.filter (option => {
return option.LOC_NA.toLowerCase().indexOf(value.toLowerCase()) === 0 })))
}
我正在尝试使用 angular material 自动完成功能在值更改时填充下拉列表,但我不断收到以下错误。
Type 'Observable<{ LOC_NA: any; }>' is missing the following properties from type
'Location[]': length, pop, push, concat, and 25 more.
下拉菜单应该填充来自 json 文件的数据。我使用以下文档作为参考 https://material.angular.io/components/autocomplete/examples
请找到下面的代码:
HTML
<input type="text" placeholder={{constant.RENTALLOCATION}} matInput
formControlName="filteredRentalLocation" [matAutocomplete]="auto"
[ngModel]="commonService.selectedRentalLocation">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of filteredLocationOptions | async" [value]="option">
{{option.CCRG_CORP_CD}} {{option.LOC_NA}}
</mat-option>
</mat-autocomplete>
TS
export interface Location {
CCRG_CORP_CD: any;
LOC_MNEM_CD: any;
CITY_NA: any;
LOC_NA: any;
}
export class CreateReservationHomeComponent implements OnInit {
filteredLocationOptions: Observable<Location[]>;
ngOnInit() {
this.initCreateResForm();
//this.createresService.fetchLocationDetails().pipe(map((res:any) => res.json()))
this.filteredLocationOptions =
this.createResForm.get('filteredRentalLocation')!.valueChanges
.pipe(
startWith(''),
map(value => this._filterGroup(value)))
}
private _filterGroup(value: string): Location[]{
if(value){
return this.createresService.fetchLocationDetails()
.pipe(map(locs => ({LOC_NA: locs.LOC_NA }))) <--- //error here
}
}
服务技术支持
locationDetailsURL = url + 'assets/Locations/aulocs.json';
fetchLocationDetails(): Observable<Location[]>{
return this.http.get<Location[]>(this.locationDetailsURL, {withCredentials: true})
JSON数据
[
{
"LOC_MNEM_CD":"XYZ",
"CCRG_CORP_CD":"B",
"CITY_NA": "sddfs",
"LOC_NA": "werwer",
},
{
"LOC_MNEM_CD":"MCD",
"CCRG_CORP_CD":"A",
"CITY_NA": "werwer",
"LOC_NA": "wwew",
}
]
我明白了。我需要过滤 locs 数组。
private _filterGroup(value: string): Observable<Location[]>{
return this.createresService.fetchLocationDetails()
.pipe(map(locs => locs.filter (option => {
return option.LOC_NA.toLowerCase().indexOf(value.toLowerCase()) === 0 })))
}