I currently understand that [(ngModel)]="expression"
is two-way binding from component to view and vice versa. I also understand that [ngModel]="expression"
is one-way binding (I believe from component to view?). Then there's the possibility of (ngModel)="expression"
. I am mostly confused as to the differences between [ngModel]
vs (ngModel)
. Could someone please explain?
EDIT: After playing around with, and reviewing the document snippet given by @Rohan Fating I realized that something like (ngModel)
should take a statement, rather than an expression. That being said, would there ever be an appropriate time to use something like (ngModel)
or would that even work in any circumstance?
[ngModel] evaluates the code and generates an output (without two-way binding). [(ngModel)] evaluates the code and generates an output and also enables two-way binding.
The ngModel directive is a directive that is used to bind the values of the HTML controls (input, select, and textarea) or any custom form controls, and stores the required user value in a variable and we can use that variable whenever we require that value. It also is used during form validations.
The NgModel class has the update property with an EventEmitter instance bound to it. This means we can't use (ngModelChange) without ngModel . Whereas the (change) event can be used anywhere, and I've demonstrated that above with the [value] property binding instead.
Because no built-in HTML element follows the x value and xChange event pattern, two-way binding with form elements requires NgModel .
This syntax:
[(ngModel)]="expression"
is unwrapped by the compiler into
[ngModel]="expression" (ngModelChange)="expression=$event"
which means:
expression
parameterngModelChange
Now you can see that [ngModel]
part is always there which as you noted will sync values down.
What happens when you specify (ngModel)="c()"
is interesting. Normally, the (...)
syntax is for events. So indeed Angular recognizes this as event and create appropriate listener in the component view factory:
function (_v, en, $event) {
var ad = true;
var _co = _v.component;
...
if (('ngModel' === en)) {
var pd_4 = (_co.c() !== false);
ad = (pd_4 && ad);
}
return ad;
}
However, all (...)
elements are also parsed as attributes, and so it will match the ngModel
directive selector:
selector: '[ngModel]:not([formControlName]):not([formControl])'
so Angular will also initialize the ngModel
directive class as a directive. However, since it doesn't have any input bindings defined through [...]
syntax this directive will be skipped during change detection. And since also the event ngModel
is not defined for the ngModel
directive the usage (ngModel)
will have no side effects and is meaningless.
[(
in Angular is signalling a two-way data binding. Theoretically you could only bind to an event ((ngModel)
) or to a value ([ngModel]
). This gives you the ability to handle changes going down in a different way than changes coming up. With Angular 2 you have that flexibility.
You need to ngModelChange event and update value like I did below
Plunker link: https://plnkr.co/edit/RZMgM69doSHGBG1l16Qf?p=preview
@Component({
selector: 'my-app',
template: `
<div>
<input [ngModel]='value' (ngModelChange)='setModel($event)'/>
<h2>Hello {{name}}</h2>
</div>
`,
})
export class App {
name:string;
value: string = '';
constructor() {
this.name = `Angular! v${VERSION.full}`
}
setModel(value) {
this.value = value;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With