类型为“string”的参数不能分配给类型为“formcontrol”的参数

bvhaajcl  于 2021-10-10  发布在  Java
关注(0)|答案(0)|浏览(235)

这是我的进口货

import { Component, OnInit, OnChanges } from '@angular/core';
import { HttpService } from ".././http.service";
import { FormControl, Validators } from "@angular/forms";

这就是我初始化表单的方式

emailFormControl = new FormControl("", [
    Validators.required,
    Validators.email
  ]);

  nameFormControl = new FormControl("", [
    Validators.required,
    Validators.minLength(4)
  ]);

这是我的发送功能

send() {
    this.loading = true;
    this.buttonText = "Submiting...";
    let user = {
      name: this.nameFormControl.value,
      email: this.emailFormControl.value
    }
    this.http.sendnotif("http://localhost:8080/send-notif", user).subscribe(
      data => {
        let res:any = data;
        console.log(
          `an email has been sent successfully to  ${user.name}  and the message id is ${res.messageId}`
        );
      },
      err => {
        console.log(err);
        this.loading = false;
        this.buttonText = "Submit";
      },() => {
        this.loading = false;
        this.buttonText = "Submit";
      }
    );
  }
}

这是html

<form style="width: 50%; margin: auto">
  <mat-card>
    <h1>Please fill the form </h1>

    <mat-form-field style="width: 100%">
      <input matInput placeholder="Name" [formControl]="nameFormControl" />
      <mat-error *ngIf="nameFormControl.hasError('minlength')">
        Name must be at least 4 characters long.
      </mat-error>
      <mat-error *ngIf="nameFormControl.hasError('required')">
        Name is <strong>required</strong>
      </mat-error>
    </mat-form-field>

    <mat-form-field style="width: 100%">
      <input matInput placeholder="Email" [formControl]="emailFormControl"/>
      <mat-error *ngIf="emailFormControl.hasError('email')">
        Please enter a valid email address
      </mat-error>
      <mat-error *ngIf="emailFormControl.hasError('required')">
        Email is <strong>required</strong>
      </mat-error>
    </mat-form-field>

    <button
      mat-raised-button
      [disabled]="emailFormControl.invalid || nameFormControl.invalid || loading"
      color="primary"
      (click)="send()"
    >
      {{buttonText}}
    </button>
  </mat-card>
</form>

我收到的错误是“字符串类型的参数不可分配给formcontrol类型的参数”,并且它指向“on”http://localhost:8080/send-如果“有什么建议我可以解决这个问题,我几乎什么都试过了。”

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题