angular - How to make a reusable two-way pipe? -
from understand there no way in angular use pipes on two-way databinding. essentially, is:
<input name="humidity" [(ngmodel)]="humidity | percent"> coming background of programming .net wpf used able have data transformed in both way (from model ui, , ui model) using value converters.
if instead change code to:
<input name="humidity" [ngmodel]="humidity | percent" (ngmodelchange)="humiditychanged($event)" > ...i'm able manually in code make backward "pipe transformation" before storing data model. please see my plunker full code sample using approach.
my questions are:
is there better approach in current or upcoming version of angular?
if want backward-pipe-transformation reusable (in same manner percent pipe reusable), way create percentcomponent containing
<input>along needed code?
angular not support data converters (i.e. two-way pipes) , there no indication of future support.
you can create custom reusable class containing transformation logic (transform(..) , transformback(..)). still, need extend model instance new property everywhere want use it.
angular pipes direct counterparts of angularjs filters , suppose transform view data. one-way, part of one-way data flow angular propagates.
if value supposed changed both in view , model, should changed in model only, automatically updated in view. consists role of model single source of truth.
humidity | percent supposed used when value should shown percent in view remain number in model.
for instance, different in aurelia shares lot of ideas angular has value converters instead of pipes, (as name suggests) capable of converting values in both directions.
Comments
Post a Comment