ERROR TypeError:“无法在”signUpForm“上分配给属性”validator“:不是对象”

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ERROR TypeError:“无法在”signUpForm“上分配给属性”validator“:不是对象”相关的知识,希望对你有一定的参考价值。

错误TypeError:“无法在”注册表单“上分配给属性”验证器“:不是对象”

我尝试了在同一个问题中回答的所有解决方案,但没有奏效。 "can't assign to property "validator" on "formControlAnimalSelect": not an object" Angular Typescript

我还检查了我的TypeScript代码中的FormGroup和html中的表单元素之间的链接,这是正确的。

 //typescript
import { Component, OnInit } from '@angular/core';
  import {FormBuilder, FormGroup, Validators} from '@angular/forms';
  import {AuthService} from '../../services/auth.service';
  import {Router} from '@angular/router';

  @Component({
  selector: 'app-signup',
  templateUrl: './signup.component.html',
  styleUrls: ['./signup.component.scss']
})
export class SignupComponent implements OnInit {

  signUpForm: FormGroup;
  errorMessage: string;

  constructor(private formBuilder: FormBuilder,
              private authService: AuthService,
              private router: Router) { }

  ngOnInit() {
    this.initForm();
  }

  initForm() {
    this.signUpForm = this.formBuilder.group({
      email: ['', [Validators.required, Validators.email]],
      password: ['', [Validators.required, Validators.pattern
      (/[0-9a-zA-Z]{6,}/)]]
    });
  }

  onSubmit() {
    const email = this.signUpForm.get('email').value;
    const password = this.signUpForm.get('password').value;
    this.authService.createNewUser(email, password).then(
      () => {
        this.router.navigate(['/books']);
      },
    (error) => {
      this.errorMessage = error;
    }

    );
  }


}
<html>
<div class="row">
  <div class="col-sm-8 col-sm-offset-2">
    <h2>Create an account</h2>
    <form formGroup="signUpForm" (ngSubmit)="onSubmit()">
      <div class="form-group">
        <label for="email"> Email </label>
        <input type="text" id="email" class="form-control"
               formControlName="email">
      </div>

      <div class="form-group">
        <label for="password"> Password </label>
        <input type="password" id="password"
               class="form-control" formControlName="password">
      </div>
      <button class="btn btn-primary" type="submit"
              [disabled]="signUpForm.invalid"> Create account</button>
    </form>
    <p class="text-danger">{{errorMessage}}</p>
  </div>
</div>

答案

您的HTML应该是这样的

试试这个:

<html>
<div class="row">
  <div class="col-sm-8 col-sm-offset-2">
    <h2>Create an account</h2>
    <form [formGroup]="signUpForm" (ngSubmit)="onSubmit()">
      <div class="form-group">
        <label for="email"> Email </label>
        <input type="text" id="email" class="form-control"
               formControlName="email">
      </div>

      <div class="form-group">
        <label for="password"> Password </label>
        <input type="password" id="password"
               class="form-control" formControlName="password">
      </div>
      <button class="btn btn-primary" type="submit"
              [disabled]="signUpForm.invalid"> Create account</button>
    </form>
    <p class="text-danger">{{errorMessage}}</p>
  </div>
</div>
另一答案

您必须错过绑定FormGroup,如果您必须添加此代码,那么为什么会出现错误。

HTML:

<div class="row">
  <div class="col-sm-8 col-sm-offset-2">
    <h2>Create an account</h2>
    <form [formGroup]="signUpForm" (ngSubmit)="onSubmit()">
      <div class="form-group">
        <label for="email"> Email </label>
        <input type="text" id="email" class="form-control"
               formControlName="email">
      </div>

      <div class="form-group">
        <label for="password"> Password </label>
        <input type="password" id="password"
               class="form-control" formControlName="password">
      </div>
      <button class="btn btn-primary" type="submit"
              [disabled]="signUpForm.invalid"> Create account</button>
    </form>
    <p class="text-danger">{{errorMessage}}</p>
  </div>
</div>

您还可以查看链接:

https://stackblitz.com/edit/angular-buks6o?file=src%2Fapp%2Fapp.component.ts

以上是关于ERROR TypeError:“无法在”signUpForm“上分配给属性”validator“:不是对象”的主要内容,如果未能解决你的问题,请参考以下文章

如何解决未处理的拒绝 TypeError: Converting circular structure to JSON for jwt.sign

code sign error 1

ERROR:TypeError: $(...).selectpicker 不是使用 requirejs 的函数加载库

python keep_1st_sign_dig_in error.py

https Azure 网站在通过 Firefox 访问时返回 SEC_ERROR_OCSP_INVALID_SIGNING_CERT [关闭]

TypeError: Error #1006: value 不是函数。