I want to iterate a map and display the keys and values in my html file, for doing that I used Angular's *ngfor with keyvalue pipe.
However, there's an error with the ngFor iteration:
Argument type Map<string, BarcodeInfoModel> is not assignable to parameter type {[p: string]: (() => IterableIterator) | ((key: string, value: BarcodeInfoModel) => this) | ((key: string) => boolean) | string | ((key: string) => (BarcodeInfoModel | undefined)) | (() => IterableIterator<[string, BarcodeInfoModel]>) | number | (() => IterableIterator) | (() => void) | ((callbackfn: (value: BarcodeInfoModel, key: string, map: Map<string, BarcodeInfoModel>) => void, thisArg?: any) => void)} | Map<string, (() => IterableIterator) | ((key: string, value: BarcodeInfoModel) => this) | ((key: string) => boolean) | string | ((key: string) => (BarcodeInfoModel | undefined)) | (() => IterableIterator<[string, BarcodeInfoModel]>) | number | (() => IterableIterator) | (() => void) | ((callbackfn: (value: BarcodeInfoModel, key: string, map: Map<string, BarcodeInfoModel>) => void, thisArg?: any) => void)>
Here the Map is declared and initialized with private files: Map<string, BarcodeInfoModel> = new Map<string, BarcodeInfoModel>();
where BarcodeInfoModel
is a customized type, with variables like this:
export class BarcodeInfoModel {
_codeType: string;
_receivingID: string;
_cache: Array<string>;
_dcr: string;
_packageType: string;
_condition: string;
constructor() {}
...
}
And in my html
I use the iteration like:
<mat-accordion>
<mat-expansion-panel *ngFor="let file of files | keyvalue">
<mat-expansion-panel-header>
<mat-panel-title>
{{file.key}}
</mat-panel-title>
</mat-expansion-panel-header>
<p>{{file.value}}</p>
</mat-expansion-panel>
</mat-accordion>
And that's where the error occurs. If I run this snippet in my browser, files.value
produces [object Object]
, not indicating it is of BarcodeInfoModel
type. Also, in the html
, I cannot get attributes of BarcodeInfoModel by doing something like {{file.value._codeType}}
.