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
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.
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.
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.
[ngModelOptions]="{standalone: true}" checks all the checkbox for angular 6. 0. 5. 1. Conflict using name and value property in input simulty.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With