salgaf
salgaf

Reputation: 201

Angular reactive form data conversion

I have data coming from server that are formatted in meters but in the interface I need to display and edit in inches then send it back to server again in meters. There is some technique to handle the conversion dynamically, say using a converter function bound to formControl or whatever? I'm using angular material components.

Thanks in advance

Upvotes: 1

Views: 777

Answers (2)

abhay
abhay

Reputation: 127

One of the best way is to actually make use of a directive to do your conversion to inches so that you don't have to worry about the server data.

something like this:

<form [formGroup]="newForm" (ngSubmit)="onSubmit()">
 <input formatData formControlName="data"/>
</form>

onSubmit() {
// ...conversion back to meters
}

I am using reactive forms as that is more dynamic and something that I use a lot as I build my forms dynamically but you can also do the same with template forms.

Upvotes: 1

MAYANK17
MAYANK17

Reputation: 41

You could use Angular Pipes to transform the data and then display them in your Interface (view).

Pipes are angular decorated class that implements a transform function which takes in data as input and transforms it to a desired output and show them to users (as they can be added to your HTML). You can read more about them here, and write a Custom Pipe for your purpose.

Upvotes: 0

Related Questions