Using Angular ngmodel without name attribute

Issue

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

Solution

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!

Answered By – Logan H

Answer Checked By – Marilyn (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.