I have an class with an array of objects:
export class MyObject {
sections: Section[]
}
The Section class contains a Map:
export class Section {
dataPoints: Map<string, string>
}
I want to present an object of class MyObject to the user of the frontend and let the user change its content. I have a problem in the HTML code for the component for that:
<ul>
<li *ngFor="let section of this.myObject; index as sectionsIndex">
<ul>
<li *ngFor="let dataPoint of section.dataPoints | keyvalue">
<mat-form-field appearance="fill">
<mat-label>Label:</mat-label>
<input matInput placeholder="Label input field placeholder"
[(ngModel)]="this.myObject.sections[sectionsIndex].dataPoints[dataPoint.key]">
</mat-form-field>
<mat-form-field appearance="fill">
<mat-label>Content:</mat-label>
<input matInput placeholder="Content input field placeholder"
[(ngModel)]="this.myObject.sections[sectionsIndex].dataPoints[dataPoint.value]">
</mat-form-field>
</li>
</ul>
</li>
</ul>
Without the keyvalue pipe I get this error in the console: "Error: NG02200: Cannot find a differ supporting object '[object Map]' of type 'object'. NgFor only supports binding to Iterables, such as Arrays. Did you mean to use the keyvalue pipe?".
I followed How to iterate using ngFor loop Map containing key as string and values as map iteration which brought me to the keyvalue pipe, but accessing the original object via [(ngModel)]="this.myObject.sections[sectionsIndex].dataPoints[dataPoint.key]"
results in a compiler error "Element implicitly has an 'any' type because type 'Map<string, string>' has no index signature. Did you mean to call 'get'?" as well as the same error message with "Did you mean to call 'set'?" at the end.
The user should be able to change the content of the dataPoints
Map in Sections
as well as other class variables of MyObject
which I didn't mention in the code example, as well as add and remove Sections from the array and add and remove elements from the Map dataPoints
by using add / remove buttons that I left out in the code as well to concentrate on the problem.
What is best practice to deal with the problem of changing an array of objects with a Map? What leads to the error in my code?