IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Angular Discussion :

NullInjectorError: No provider for FormGroup!"


Sujet :

Angular

  1. #1
    Membre actif
    Profil pro
    Inscrit en
    Septembre 2006
    Messages
    728
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2006
    Messages : 728
    Points : 250
    Points
    250
    Par défaut NullInjectorError: No provider for FormGroup!"
    Bonjour, je développe une application angular. J'ai implementé un composant de dialogue qui est le suivant:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
        import { Component, OnInit, Inject } from '@angular/core';
        import {MatDialogRef, MAT_DIALOG_DATA} from '@angular/material/dialog';
        import { FormGroup, FormControl, Validators } from '@angular/forms';
        import {MAT_RADIO_DEFAULT_OPTIONS } from '@angular/material';
     
        @Component({
          selector: 'app-user-dialog',
          templateUrl: './user-dialog.component.html',
          styleUrls: ['./user-dialog.component.scss'],
          providers: [
            {provide: MAT_RADIO_DEFAULT_OPTIONS, useValue: { color: 'accent' }},
            { provide: MatDialogRef, useValue: {} }
            // ,{ provide: MAT_DIALOG_DATA, useValue: [] }
          ]
        })
        export class UserDialogComponent implements OnInit {
     
     
          public form: FormGroup;
     
          private validateAreEqual(fieldControl: FormControl) {
            return fieldControl.value === this.form.get('password').value ? null : {
                NotEqual: true
            };
          }
     
          constructor(
            private dialogRef: MatDialogRef<UserDialogComponent>,
            @Inject(MAT_DIALOG_DATA) data) {
             }
     
          ngOnInit() {
            this.form = new FormGroup({
              gender: new FormControl('', Validators.required),
              lastName: new FormControl('', Validators.required),
              firstName: new FormControl('', Validators.required),
              email: new FormControl('', Validators.compose([Validators.email, Validators.required])),
              password: new FormControl('', Validators.compose([Validators.required, Validators.pattern('(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&].{8,}')])),
              // tslint:disable-next-line: max-line-length
              confirmPassword: new FormControl('', Validators.compose([Validators.required, Validators.pattern('(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&].{8,}'), this.validateAreEqual.bind(this)]))
            });
          }
     
          save() {
            this.dialogRef.close(this.form.value);
          }
     
          close() {
            this.dialogRef.close();
          }
     
        }
    Je l'appelle depuis une méthode d'un autre composant:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
        openDialog() {
          console.log('ca passe');
          const dialogConfig = new MatDialogConfig();
          dialogConfig.disableClose = true;
          dialogConfig.autoFocus = true;
          dialogConfig.position = {
            top:  '0',
            right: '0'
          };
          dialogConfig.width = '50%' ;
          dialogConfig.height = '350px' ;
          console.log(dialogConfig);
          this.dialog.open(UserDialogComponent, dialogConfig);
        }
    Mais j'ai l'erreur suivante:

    ERROR NullInjectorError: "StaticInjectorError(AppModule)[UserDialogComponent -> FormGroup]:
    StaticInjectorError(Platform: core)[UserDialogComponent -> FormGroup]:
    NullInjectorError: No provider for FormGroup!"
    Pouvez vous m'aider ?

  2. #2
    Membre actif
    Profil pro
    Inscrit en
    Septembre 2006
    Messages
    728
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2006
    Messages : 728
    Points : 250
    Points
    250
    Par défaut
    Bonjour, finalement, j'ai trouvé une manière d'instancier FormGroup. Voici la solution solution


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
     
        export class UserDialogComponent implements OnInit {
     
     
          public dialogFormGroup: FormGroup;
     
     
     
          constructor(
            private formBuilder: FormBuilder,
            private dialogRef: MatDialogRef<UserDialogComponent>,
            @Inject(MAT_DIALOG_DATA) data) {
             }
     
          ngOnInit() {
            this.dialogFormGroup = this.formBuilder.group({
              gender: ['', Validators.required],
              lastName: ['', Validators.required],
              firstName: new FormControl('', Validators.required),
              email: ['', Validators.compose([Validators.email, Validators.required])],
              password: ['', Validators.compose([Validators.required, Validators.pattern('(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&].{8,}')])],
              // tslint:disable-next-line: max-line-length
              confirmPassword: ['', Validators.compose([Validators.required, Validators.pattern('(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&].{8,}'), this.validateAreEqual.bind(this)])]
            });
          }
    Pouvez vous m'expliquer pourquoi j'ai besoin d'un FormBuilder et ne peux pas directement instancier FormGroup ( new FormGroup({...

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 19
    Dernier message: 10/04/2008, 15h52
  2. télécharger oracle odbc provider for microsoft
    Par edzodzinam dans le forum Oracle
    Réponses: 1
    Dernier message: 02/02/2007, 16h14
  3. Réponses: 1
    Dernier message: 26/10/2006, 17h03
  4. Microsoft OLE DB Provider for ODBC Drivers error
    Par tribaleur dans le forum ASP
    Réponses: 2
    Dernier message: 31/05/2006, 09h52
  5. Réponses: 3
    Dernier message: 12/10/2004, 23h45

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo