Reputation: 2852
To bind the value of an input to a property, we use the ngModel directive. For example:
<input type='text' [(ngModel)]='model' />
Why can't we simply use binding on the value
property of the input element?
<input type='text' [(value)]='model' />
Upvotes: 4
Views: 5068
Reputation: 658087
You can do
<input type='text' [value]='model' (input)="model=$event" />
[(value)]='model'
doesn't work because the <input>
doesn't emit a valueChange
event.
ngModel
also provides forms integration which direct value binding doesn't.
See also https://angular.io/docs/ts/latest/guide/template-syntax.html#!#two-way
ngModel
uses provided ControlValueAccessor
s, which are directives provided for all kinds of input elements (can also be a custom one for your own components) that acts as an adapter between ngModel
and any component. This is to unify binding with all kinds of components and input elements.
Upvotes: 5
Reputation: 55443
You can do it but...
ngModel
directive allows you to use one-way
or two-way
binding according to your need. ngModel
directive through which you can see whether any of form controls is valid
or not and so form
is valid or not.ngModel
directive is built with Observable
and Observer
pattern which allows you to emit stream of data over the time.In short there are lots of benefits given with ngModel
directive so usually people prefer to use ngModel
over value
syntax.
Upvotes: 2
Reputation: 86800
you can , but using [value]
only like this
<input type='text' [value]='model' />
we use [(ngModel)]
because it is a two way data binding, whereas [value]
is one way binidng
when we use []
alongwith ()
it means it is two way binding.
apart from simply (input)
there are no. of events are there in angular2 like mouseover
mouseout
etc.
Upvotes: 1