I know Angular2 doesn't have two-way data binding but is there a way to mimick the two-way data binding behavior from Angular1.x?
相关问题
- Angular RxJS mergeMap types
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- How to update placeholder text in ng2-smart-table?
- How to instantiate Http service in main.ts manuall
- Angular: ngc or tsc?
相关文章
- angular脚手架在ie9+下兼容问题
- angular 前端项目 build 报错 "Cannot find module 'le
- Angular Material Stepper causes mat-formfield to v
- After upgrade to Angular 9 cannot find variable in
- is there any difference between import { Observabl
- Suppress “Circular dependency detected” suppress w
- How can you get current positional information abo
- Angular material table not showing data
Note - scroll down the answer for ng-model binding
You could actually do that, just that you need to invoke internal changelistener tick (similar to digest) to update binding in the zone, You can just add a
(keyup)
event for that. Similarly you could use directive bindings as well withproperties
dictionary of component settings.Example:-
Display as:
Parent Component has a textbox and a label.
Now displaying it in child component as well:
Demo
Update - ng-model for 2-way binding
Though Angular2 is one-time bound by default,
ngModel
sugar has been introduced to achieve 2-way binding. With that you could do for instance:Here usage of square brackets (
[..]
) suggests the property binding and round brackets ((..)
) for event binding. Basically when you useng-model
, you are enabling both the bindingsngModel
is more of an event. Behind the scenes it creates an observable event(withEventEmitter
) to track thevalue
changes in the bound element and update the bound property respectively. For example:-Include formDirectives:
and with form
without form
not necessary anymore
include formDirectives dependency in view annotation.Demo
Also read the nice write up from Victor Savkin on Two-way binding in angular2 by creating the ng-model event and how it works.
There is another way to trick Angular2 into two-way binding. Don't pass a property but an object into the component. If you pass an object via one-way binding all of its properties are in fact two-way bound. It makes the component less versatile as it needs to know the object but in many cases it's still useful.
I have a component that looks like this:
It is invoked like this:
The movie object itself is one-way bound but all of its properties can be used for two-way binding.
Its simple, try this;
Yes there is two-way binding in angular2. See here: https://angular.io/docs/ts/latest/guide/template-syntax.html#!#ngModel
So, how to use it in custom components?
What I like to do is something this:
And use it like
You can also emit the new value where it is actually changed. But I find it quite convenient to do that gloabaly in a setter method and don't have to bother e.g. when I bind it directly to my view.
Here is a simple plunker which demonstrates one way, two way and event driven approaches in action according to Angular2 2.0.0-beta.17
http://plnkr.co/eXZMoU
Two-way Event and property
One way property
Event driven
We can dig Angular docs for more
You can do this by attaching to the events on the input field and updating the internal value as is done in this example:
http://plnkr.co/edit/lOFzuWtUMq1hCnrm9tGA?p=preview
Create a component that has an internal attribute that holds the label
this.label
and a callbackchangeLabel
that expects an event objectThe create your template and attach the callback to the appropriate event (you could attach it to the
keypress
event but then you might need a timeout. I attached it to thechange
event for simplicity (which means you might need to tab off the input to see the update).