Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a reusable two-way pipe?

Tags:

angular

From what I understand there is no way in Angular to use pipes on a two-way databinding. Essentially, what I would like to do is:

<input name="humidity" [(ngModel)]="humidity | percent">

Coming from a background of programming .NET WPF I am used to be able to have data transformed in both way (from model to UI, and from UI to model) using value converters.

If I instead change the code to:

<input name="humidity" [ngModel]="humidity | percent" (ngModelChange)="humidityChanged($event)" >

...I'm able to manually in code make the backward "pipe transformation" before storing the data to the model. Please see my Plunker for full code sample using this approach.

My questions are:

  1. Is there a better approach in the current or upcoming version of Angular?

  2. If I want the backward-pipe-transformation to be reusable (in the same manner as the percent pipe is reusable), is it a good way to create a PercentComponent containing an <input> along with the needed code?

like image 498
coder925 Avatar asked Aug 15 '17 14:08

coder925


People also ask

What is 2way binding?

In a two-way binding, the data flow is bi-directional. This means that the flow of code is from ts file to Html file as well as from Html file to ts file. In order to achieve a two-way binding, we will use ngModel or banana in a box syntax.

Can we use multiple pipes?

Reuse Pipes In Multiple Components: It's is not necessary that we should use the pipe in a single component because we can reuse the pipe in multiple components.

How Angular pipes work?

By default, pipes are defined as pure so that Angular executes the pipe only when it detects a pure change to the input value. A pure change is either a change to a primitive input value (such as String , Number , Boolean , or Symbol ), or a changed object reference (such as Date , Array , Function , or Object ).

How do you use custom pipes?

Steps Involved In Creating a Custom Pipe In Angular are: 1) Create a Pipe Class and decorate it with the decorator @Pipe. 2) Supply a name property to be used as template code name. 3) Register your Pipe in the module under declarations. 4) Finally, implement PipeTransform and write transformation logic.


1 Answers

Angular do not support data converters (i.e. two-way pipes) and there is no indication of any future support.

You can create a custom reusable class containing the transformation logic (transform(..) and transformBack(..)). Still, you will need to extend you model with for instance a new property everywhere where you want to use it.

Angular pipes are direct counterparts of AngularJS filters and suppose to transform view data. They are one-way, as a part of one-way data flow that Angular propagates.

If a value is supposed to be changed both in view and a model, it should be changed in model only, then it will be automatically updated in view. This consists with a role of a model as single source of truth.

humidity | percent is supposed to be used when a value should be shown with a percent in view but remain a number in model.

For instance, this is different in Aurelia which shares a lot of ideas with Angular but has value converters instead of pipes, they (as the name suggests) are capable of converting values in both directions.

like image 74
Estus Flask Avatar answered Oct 26 '22 14:10

Estus Flask