javascripthtmlangulartypescriptangular-ngmodel

Why ngModel doesn't works on the last version of Angular 17?


I am trying to make a form in my angular app, but when i want to implement ngModel on my form :

    <form (ngSubmit)="onSignUp()" #signupForm="ngForm">
        <h1>Connexion</h1>
        <input type="email" name="mail" [(ngModel)]="userLogin.email" placeholder="Email" />
        <input type="password" name="mdp" [(ngModel)]="userLogin.password" placeholder="Password" />
        <a href="#">Mot de passe oublie ?</a>
        <button type="submit">Se Connecter</button>
    </form>

I have this error :

NG8002: Can't bind to 'ngModel' since it isn't a known property of 'input'. [plugin angular-compiler]

I can't import FormsModule in the app.module.ts because this file doesn't exists on Angular 17, i only have an app.config.ts file.

Can someone please explain me how to do?


Solution

  • If your component is set with standalone: true, then you need to add FormsModule to the imports array of the component!

    Since it's standalone, we need to add all the necessary dependencies to the imports array!

    @Component({
      ...
     imports: [
          ...
          FormsModule,
          ...
      ],
      ...
    })