Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Angular ngmodel without name attribute

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

like image 786
elahe kamaliha Avatar asked Oct 05 '16 18:10

elahe kamaliha


People also ask

Can ngModel be used without form?

Can I use ngModel without form? See the example for using NgModel as a standalone control. standalone: When set to true, the ngModel will not register itself with its parent form, and acts as if it's not in the form.

What is [( ngModel )]?

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.

Can't bind to ngModel since it isn't a known property?

To fix Can't bind to 'ngModel' since it isn't a known property of 'input' error in Angular applications we have to import FormModule in app. module. ts file. If you are using FormBuilder class to create reactive form we have to import ReactiveFormsModule as well to avoid below error.

What is ngModelOptions ]= standalone true?

[ngModelOptions]="{standalone: true}" checks all the checkbox for angular 6. 0. 5. 1. Conflict using name and value property in input simulty.


1 Answers

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!

like image 76
Logan H Avatar answered Oct 13 '22 01:10

Logan H