7

In Angular 2.x, is there a difference between ng-model and NgModel in Angular 2? Referring to following Angular documentation

https://angular.io/docs/ts/latest/guide/architecture.html

It uses terminology [(ng-model)]="property" to explain two-way data binding. Just below this, it uses the example using NgModel (instead of ng-model)

<input [(ngModel)]="hero.name">

I am confused if this is a typo (I guess not!).

Rakesh Chand
  • 3,105
  • 1
  • 20
  • 41
Manu Chadha
  • 15,555
  • 19
  • 91
  • 184

2 Answers2

9

[ is component bind to view . ( is view bind to component . ngModel is two way binding so this use this syntax [( . And there is no differentce beetween [(ngModel]) in angular2+ and ng-model in angular1

Akashii
  • 2,251
  • 3
  • 17
  • 29
7

No, there is no difference, but the one in Angular gives you more flexibility than the one in AngularJS.

[( 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 AngularJS you do not have that flexibility.

But just to sum up, ng-model is equal to [(ngModel)].

Vladimir Zdenek
  • 2,270
  • 1
  • 18
  • 23
  • Are you saying I can use any one of them in the code? They are interchangeable in the code. – Manu Chadha Jun 01 '17 at 06:39
  • 2
    No. You can use ``ng-model`` with AngularJS and ``[(ngModel)]`` with Angular (v2+). It depends on which one you want to use. They are completely different from each other. – Vladimir Zdenek Jun 01 '17 at 06:40
  • thank you for clarifying the relationship between `ng-model` and `[(ng-model)]` and the discrepancies between Angular and AngularJS relating to this directive – Darrel Holt Nov 14 '17 at 19:01