I'm trying to get this component working that will take an object of key/value pairs, display them in input tags, and change them on the original object. So say I have an object like:
{
name: "John",
id: "12345",
preferences: { foo: "bar", biz: "baz" }
}
I can call <key-value [obj]="profile.preferences"></key-value> and the data will be printed in two pairs in input tags. If I then changed "foo" to "qux" the property key would change. To accomplish this I have a key-value component that really simply does:
<div *ngFor="let pref of obj | keyvalue">
<input [(ngModel)]="pref.key"><br />
<input [(ngModel)]="pref.value"><br />
</div>
Which I feel is very simple. It's an object passed in, so it's a reference to the original object, and the ngModels in literally every other input of my application work so I assume that's not wrong.
I've done a lot of looking around and I did actually get it working if I had a "changeKey/changeValue" function that would change this.obj in the component rather than relying on binding:
<div *ngFor="let pref of obj | keyvalue">
<input (change)="changeKey(/*???*/)" [ngModel]="pref.key"><br />
<input (change)="changeValue(pref.key, pref.value) [ngModel]="pref.value"><br />
</div>
That works for the changeValue because I have the key I need to change and the new value. But the issue with the changeKey is because pref.key has been changed I don't know which key to update.
I feel like this shouldn't be that difficult, I'm still quite new to Angular and I'm hoping someone here knows what's going on. Thanks.