6

I am just reading and learning Angular2 tutorial. Here it says "Defining a name attribute is a requirement when using [(ngModel)] in combination with a form." However, in the tutorial, it is using ngmodel without a name attribute here.

 <input [(ngModel)]="selectedHero.name" placeholder="name"/>

Could you please explain why this is working? Please note : I am new to angular as well as UI

  • 1
    you have answered your own question, if you read carefully it says Defining a name attribute is a requirement when using [(ngModel)] in **combination with a form**. in the example discussed it does not uses a Form. – Madhu Ranjan Oct 05 '16 at 18:50

1 Answers1

1

I do believe that in your first link HERE they aren't using a <form></form> element.

Because in your 2nd link HERE they say

Defining a name attribute is a requirement when using [(ngModel)] in combination with a form.

... in combination with a form. In their conclusion files, hero-form.component.html they have a <form> wrapped around the input with the name attribute.

Now I don't know why the <form></form> element makes a difference on why you need the name attribute but that is why your code is working, because in your first link and in your example there isn't a form around the element.

Good question though, I just learned that, thanks to you having me look it up!

Logan H
  • 3,383
  • 2
  • 33
  • 50
  • I would suggest HERE since it makes more sens. However if we pick the HERE one we may face some performance issues – user2080105 Jan 20 '17 at 13:26