首页 > 解决方案 > 'string' 类型的参数不可分配给'FormControl' 类型的参数

问题描述

这是我的进口

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>

我收到的错误是“'string'类型的参数不能分配给'FormControl'类型的参数。” 它指向“http://localhost:8080/send-notif”任何我如何解决这个问题的建议我几乎尝试了一切

标签: node.jsangularform-control

解决方案


推荐阅读