Angular: If ngModel is used within a form tag either the name attribute must be set

In this article, we discuss Angular: If ngModel is used within a form tag either the name attribute must be set

Angular: If ngModel is used within a form tag either the name attribute must be set

Angular: If ngModel is used within a form tag either the name attribute must be set
Angular: If ngModel is used within a form tag either the name attribute must be set

Angular: If ngModel is used within a form tag either the name attribute must be set

In a small Angular project. I have a login form as below:

<div class="">
    <div class="main d-flex align-items-center">
        <div class="container">
            <div class="row">
            <div class="col-md-8 mx-auto">
                <div class="card-group">
                <div class="card p-4">
                    <div class="card-body">
                    <form>
                        <h1>Login</h1>
                        <p class="text-muted">Sign In to your account</p>
                        <div class="input-group mb-3">
                            <div class="input-group-prepend">
                                <span class="input-group-text"><i class="icon-user"></i></span>
                            </div>
                           <input type="text" class="form-control" placeholder="Username" [(ngModel)]="username" autocomplete="username" required>
                        </div>
                        <div class="input-group mb-4">
                            <div class="input-group-prepend">
                                <span class="input-group-text"><i class="icon-lock"></i></span>
                            </div>
                           <input type="password" class="form-control" placeholder="Password"[(ngModel)]="password" autocomplete="current-password" required>
                        </div>
                        <div class="row">
                            <div class="col-6">
                                <button type="button" (click)=dologin class="btn btn-primary px-4">Login</button>
                            </div>
                        
                        </div>
                    </form>
                    </div>
                </div>
               
            </div>
        </div>
    </div>
</div>

When I run project I get an error: If ngModel is used within a form tag, either the name attribute must be set or the form control must be defined as ‘standalone’ in ngModelOptions.

you can choose one way below to resolve your issue

<input [(ngModel)]="username" name="username">

or

<input [(ngModel)]="username" [ngModelOptions]="{standalone: true}">

I hope It works

Also Check out Format date using pipe and append local time zone – Angular

More Info Ng Model

ngModel, Angular Forms, Angular Input Forms, Input Data Get, Angular Errors, name attribute, ngModelOptions, standalone, form tags

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

How to Protect yourself from CovidVaccination saves lives at every stage of life
  • Clean your home regularly, particularly frequently touched surfaces like
  • Stay home stay safe. Follow the Golden Rule. Wash your hands frequently with soap and water or use alchohol based hand-rub.
  • Stay home stay safe. Stay physically fit. Exercise regulary. Eat a nutritious diet. Don’t smoke.
  • Stay home stay safe. If you show symptoms of COVID-19, self isolate yourself, wear a mask around others and seek medical advice