I used this post to create a custom pipe and apply it to my reactive form. Applying a pipe or transform to a Reactive Form value
This works fine when loading the form, values are multiplied by a factor.
But it does not work the other way around when entering a number. E.g. when 10, with the factor beeing 1000, I would expect 10000 in {{ form.value | json }}
But this does not work. Does somebody know what I am doing wrong?
Kind regards!
Below you can see
- the pipe definition
- an excerpt of the reactive form for the field I am applying the pipe to
- the html template for that field
import {
Pipe,
PipeTransform
} from '@angular/core';
@Pipe({
name: 'scaleInput'
})
export class ScaleInputPipe implements PipeTransform {
transform(value: number, factor: number): number {
const result: number = value / factor;
return result;
}
}
...
constructor(private _scaleInputPipe: ScaleInputPipe) {}
...
this._formBuilder.group({
expectedCashFlow: this._scaleInputPipe.transform(x.expectedCashFlow, 1000),
hedgeRatio: x.hedgeRatio,
...
})
<mat-form-field>
<input formControlName="expectedCashFlow" [(ngModel)]="x.expectedCashFlow" #cf matInput type="number">
</mat-form-field>