refactor
refactor

Reputation: 15064

NgModel vs ngModel in Angular2

I am new to Angular2 , While going through blogs on Angular2 , I have come across NgModel and ngModel. I understand that [(ngModel)] is used for two way binding. Can any one explain whats the difference between the two?

Upvotes: 2

Views: 357

Answers (1)

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657476

NgModel is the class that implements the ngModel directive.
ngModel is the selector of that directive.
This means in HTML you use ngModel like

<input [(ngModel)]="someProp">

but if you need to inject it (for example for a ControlValueAccessor) you inject a class instance

constructor(private ngModel:NgModel) {}

where ngModel is just a variable name I choose and NgModel is the class where we want an instance get injected.

See also https://github.com/angular/angular/blob/26d1423ae93a743bd57a5fed9997c08c52f4bbfd/modules/%40angular/forms/src/directives/ng_model.ts#L90

The source shows that because of exportAs: 'ngModel' it can be used like

<input [(ngModel)]="someProp" let-xxx="ngModel">
{{xxx.name}}

for example to output the name property (or any other property) of the NgModel instance that was applied to our <input>.

Upvotes: 1

Related Questions