Свойство 'nome' не существует для типа 'FormGroup' - PullRequest
1 голос
/ 16 мая 2019

У меня угловое приложение, и у меня возникли проблемы с проверкой формы через ReactiveForms. У меня ошибка:

ng ошибка подачи:

src / app / pages / contact / contact.component.ts (48,32): ошибка TS2339: Свойство assunto не существует для типа FormGroup. src / app / pages / contact / contact.component.ts (50,57): ошибка TS2339: Свойство assunto не существует для типа FormGroup. src / app / pages / contact / contact.component.ts (51,30): ошибка TS2339: Свойство 'nome' не существует для типа 'FormGroup'. src / app / pages / contact / contact.component.ts (52,33): ошибка TS2339: Свойство empresa не существует для типа FormGroup. src / app / pages / contact / contact.component.ts (53,32): ошибка TS2339: Свойство 'email' не существует для типа 'FormGroup'. src / app / pages / contact / contact.component.ts (54,34): ошибка TS2339: Свойство telefone не существует для типа FormGroup.

contact.component.html

<form class="col-s4 dados-form" [formGroup]="dadosForm">

<mat-form-field style="width:100%" class="full-width">
  <input matInput placeholder="Nome" formControlName="nome" required>
  <mat-error *ngIf="dadosForm.get('nome').dirty || dadosForm.get('nome').touched">
    O campo nome deve ser preenchido</mat-error>
</mat-form-field> <br>

<mat-form-field style="width:100%" class="full-width">
  <input matInput placeholder="Empresa" formControlName="empresa" required>
  <mat-error
    *ngIf="dadosForm.get('empresa').dirty || dadosForm.get('empresa').touched">
    O campo empresa deve ser preenchido</mat-error>
</mat-form-field> <br>

<mat-form-field style="width:100%" class="full-width">
  <input matInput placeholder="E-Mail" formControlName="email" required>
  <mat-error
    *ngIf="dadosForm.get('email').dirty || dadosForm.get('email').touched">
    {{getMailErrorMessage()}}</mat-error>
</mat-form-field> <br>
<mat-form-field style="width:100%" class="full-width">
  <input matInput maxlength="15" id="phoneInput" formControlName="telefone" [mask]="phoneMask" placeholder="Telefone para Contato" required />
  <mat-error
    *ngIf="dadosForm.get('telefone').dirty || dadosForm.get('telefone').touched">
    O campo telefone deve ser preenchido</mat-error>
</mat-form-field> <br>

<mat-form-field style="width:100%" class="full-width">
  <mat-label>Produto Desejado</mat-label>
  <mat-select matInput formControlName="assunto" required>
    <mat-optgroup *ngFor="let categoria of produtos" [label]="categoria.key">
      <mat-option *ngFor="let produto of categoria.value" [value]="produto">
        {{produto}}
      </mat-option>
    </mat-optgroup>
  </mat-select>
  <mat-error
    *ngIf="dadosForm.get('assunto').dirty || dadosForm.get('assunto').touched">
    O campo assunto deve ser preenchido</mat-error>
</mat-form-field><br>

<mat-form-field style="width:100%" class="full-width">
  <textarea matInput placeholder="Mensagem" formControlName="mensagem" required></textarea>
  <mat-error
    *ngIf="dadosForm.get('mensagem').dirty || dadosForm.get('mensagem').touched">
    O campo mensagem deve ser preenchido</mat-error>
</mat-form-field><br>

<div class="form-buttons">
  <button mat-button mat-raised-button id="submitButton" [disabled]="!dadosForm.valid" matTooltip="" color="primary" (click)="sendMail(mensagem)">Enviar</button>
</div>

</form>

contact.component.ts

  dadosForm = new FormGroup({
    nome: new FormControl('', [Validators.required]),
    empresa: new FormControl('', [Validators.required]),
    email: new FormControl('', [Validators.required, Validators.email]),
    telefone: new FormControl('', [Validators.required]),
    assunto: new FormControl('', [Validators.required]),
    mensagem: new FormControl('', [Validators.required])
  });

1 Ответ

0 голосов
/ 16 мая 2019

Использовать построитель форм FormBuilder для проверок

Сначала импортировать эти зависимости

import { FormGroup, FormBuilder, Validators } from '@angular/forms';

Создать переменную для группы форм

formGroupName: FormGroup;

constructor(private _formBuilder: FormBuilder) { }

Установить код проверки в ngOnInit method

this.formGroupName = this._formBuilder.group({
    nome: ['', Validators.required],
    empresa: ['', Validators.required],
    email: ['', [Validators.required, Validators.email]],
    telefone: ['', Validators.required],
    assunto: ['', Validators.required],
    mensagem: ['', Validators.required]
});

Попробуйте с этим.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...