I'm trying to use *ngFor for a set of identical inputs save for the property the input should bind to.
<mat-form-field *ngFor="let prop of [ID, Name, Nickname]">
<input matInput type="text" placeholder="prop.key" #propInput [(ngModel)]="prop">
<button mat-icon-button matSuffix mat-icon-button *ngIf="propInput.value" (click)="prop='';">
<mat-icon>close</mat-icon>
</button>
</mat-form-field>
<!-- bind test -->
<input matInput type="text" placeholder="Name Test" #propInput [(ngModel)]="Name">
prop.key does not work, but that is a separate question.
Here is the tethered component:
import { Component } from '@angular/core';
import { EntitySrcService } from '../entity-src.service';
@Component({
selector: 'app-entity-table',
templateUrl: './entity-table.component.html',
styleUrls: ['./entity-table.component.less']
})
export class EntityTableComponent {
constructor(
private entitySrc: EntitySrcService
) {}
public get ID(): string {
return this.entitySrc.id;
}
public set ID(value: String) {
this.entitySrc.id = value;
}
public get Name(): string {
return this.entitySrc.name;
}
public set Name(value: String) {
this.entitySrc.name = value;
}
public get Nickname(): string {
return this.entitySrc.altName;
}
public set Nickname(value: String) {
this.entitySrc.altName = value;
}
}
For the most part this seems to work as expected, but it seems the [(ngModel)] bind is only reading the property and not writing to it. So for example, if I update the bind test input, the Name field in the ngFor loop updates to match, but updating the one in the ngFor loop does not update the one in the test.
I'm using get/set properties in the component to proxy to the actually storage location, but my understanding is that a get/set property should act the same as a normal property (and using a normal property has the same one way bind behavior).
So how do I properly iterate over a set of properties that I want to bind to?