I'm having troubles using a pipe in my reactive form. I have a pipe that transforms my object into an array so it can be used with *ngFor
.
So far so good... the problem is that when I create an input (using form control to bind the value) inside my *ngFor
, each change in this input triggers a pipe update. The result is that my input loses focus each time I write one letter in the input.
HTML:
<form [formGroup]="myForm" novalidate (ngSubmit)="save(myForm.value, myForm.valid)">
<div *ngFor="let item of myForm.controls.object.value | keys" formGroupName='object'>
<label>{{lang}}</label>
<input name="item" type="text" placeholder="Option" [formControlName]="item.key"/>
</div>
</form>
And my pipe:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'keys'
})
export class KeysPipe implements PipeTransform {
transform(value, args:string[]) : any {
if (!value) {
return value;
}
let keys = [];
for (let key in value) {
keys.push({key: key, value: value[key]});
}
return keys;
}
}
Here is a plunker to demonstrate the issue.
How can I make the pipe inactive when I write or can I use a different approach than a pipe?
Please note that I can't change my object and that it has unkown properties (item1
and item2
in the plunker example)