Angular 6 : TypeError: Cannot read property 'invalid' of undefined?

标签 angular angular6

我正在编写应用程序的前端代码,但我在 Angular 6 注册代码中遇到了问题。我显然犯了一个 TypeError,但我不明白为什么它会是未定义的。

这是我的 registration.component.ts。

import { Component, OnInit } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule} from '@angular/platform-browser/animations';
import { NgModule } from '@angular/core';
import {RouterModule} from '@angular/router';
import {Routes} from '@angular/router';
import {
  MatToolbarModule, MatButtonModule, MatInputModule, MatIconModule, MatSelectModule, MatTableModule, MatGridListModule,
  MatCardModule, MatMenuModule, MatFormFieldModule, MatOptionModule, MatRadioModule
} from '@angular/material';
import {HttpClientModule } from '@angular/common/http';
import {FormBuilder, FormControl, FormsModule} from '@angular/forms';
import {FormGroup} from '@angular/forms';
import {Validators} from '@angular/forms';
import {ReactiveFormsModule} from '@angular/forms';
import { LayoutModule } from '@angular/cdk/layout';
import 'hammerjs';


@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.component.css']
})
export class RegisterComponent implements OnInit {
  registerForm: FormGroup;
  loading = false;
  submitted = false;
  hide = true;
  public lastNameC: FormControl;
  public firstNameC: FormControl;
  public emailC:  FormControl;
  public passwordC: FormControl;
  handleSubmit() {
    console.log(this.registerForm.value);
    alert('You registered!');
  }

  trueOrFalse() {
    return this.registerForm.valid;
  }

  passwordError() {
    return this.passwordC.hasError('minlength') ? 'Your password is too short.' :
      this.passwordC.hasError('pattern') ? 'Your password must have one uppercase letter, one lowercase letter, one number and one non alphanumeric character.' :
        ' ';
  }

  lastNameError() {
    if (this.lastNameC.hasError('required')) {
      return 'Last name is required.';
    }
  }

  emailError() {
    return this.emailC.hasError('required') ? 'You must enter a value.' :
      this.emailC.hasError('email') ? 'Not a valid email. Please read the field again.' :
        ' ';
  }

  firstNameError() {
  if (this.firstNameC.hasError('required')) {
    return 'Enter your first name.';
  }



  }



  constructor() {

  }

  ngOnInit() {
    this.registerForm = new FormGroup({
      emailC: new FormControl('', [Validators.required, Validators.email]),
      firstNameC : new FormControl('', [Validators.required]),
      lastNameC: new FormControl('', [Validators.required]),
      passwordC : new FormControl('', [Validators.required, Validators.minLength(8), Validators.pattern('^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[$@$!%*?&])[A-Za-z\\d$@$!%*?&]{8,}')]),
    });


  }


}

<form class="example-form" [formGroup]="registerForm" (ngSubmit)="handleSubmit()">
  <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
  <mat-form-field class="example-full-width">
  <input matInput placeholder="First Name"  formControlName="firstNameC">
    <mat-error *ngIf="firstNameC.invalid">{{firstNameError()}}</mat-error>
 </mat-form-field>
  <mat-form-field class="example-full-width">
    <input matInput placeholder="Last Name" formControlName="lastNameC">
    <mat-error *ngIf="lastNameC.invalid"> {{lastNameError()}} </mat-error>
  </mat-form-field>
  <mat-form-field class="example-full-width">
    <input  matInput placeholder="email" formControlName="emailC">
    <mat-error *ngIf="emailC.invalid">{{emailError()}}</mat-error>
  </mat-form-field>
  <mat-form-field>
    <input matInput placeholder="password" formControlName="passwordC">
    <mat-error *ngIf="passwordC.invalid">{{passwordError()}}</mat-error>
  </mat-form-field>

  <button mat-raised-button color="accent"> Register </button>

</form>

我的注册表单的 html。错误发生在第 4 行,但对于我的 formControlName,我已经引用了适当的控件。

The error aforementioned..
RegisterComponent.html:4 ERROR TypeError: Cannot read property 'invalid' of undefined
    at Object.eval [as updateDirectives] (RegisterComponent.html:5)
    at Object.debugUpdateDirectives [as updateDirectives] (core.js:11062)
    at checkAndUpdateView (core.js:10459)
    at callViewAction (core.js:10700)
    at execComponentViewsAction (core.js:10642)
    at checkAndUpdateView (core.js:10465)
    at callViewAction (core.js:10700)
    at execEmbeddedViewsAction (core.js:10663)
    at checkAndUpdateView (core.js:10460)
    at callViewAction (core.js:10700)

这是我的 StackBlitz example throwing the error.

最佳答案

您需要使用如下安全导航运算符,

   <mat-error *ngIf="firstNameC?.invalid">{{firstNameError()}}</mat-error>

STACKBLITZ DEMO

关于 Angular 6 : TypeError: Cannot read property 'invalid' of undefined?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51682139/

相关文章:

angular - 错误错误 : StaticInjectorError Angular 6

Angular Material slider 更改事件不起作用

angular - 找不到导出接口(interface)的名称

Angular 2注入(inject)服务未定义

javascript - 如何找到选项卡关闭并清除本地存储

angular - 如何在 Angular 6 应用程序中通过 `ng build` 指定环境

Angular 6 - 如何使用按钮从另一个组件切换 sidenav

Angular 6 混合 i18n JIT 和 AOT

angular - 是否可以在不从 DOM 读取的情况下在 Angular 中读取元素的文本内容?

angular - 防止浏览器在 Angular 中加载拖放文件