【问题标题】:Why Am I getting this erroor : "ERROR Error: Cannot find control with unspecified name attribute"为什么我会收到此错误:“错误错误:找不到具有未指定名称属性的控件”
【发布时间】:2021-03-17 04:41:10
【问题描述】:

我正在尝试制作一个可重用的输入组件,但我在控制台上收到此错误。 “ERROR 错误:找不到具有未指定名称属性的控件”。

这是我的注册类文件: ......

 authForm = new FormGroup(
    {
      username: new FormControl(
        '',
        [
          Validators.required,
          Validators.minLength(3),
          Validators.maxLength(20),
          Validators.pattern(/[⌃a-z0-9]+$/),
        ],
   ......

这是我的注册模板:

<form [formGroup]="authForm" class="ui form">
    <app-input
      label="Username"
      [control]="authForm.get('username')"
    ></app-input>

  .......

这是我的输入类文件:

import { Component, OnInit, Input } from '@angular/core';

import { FormControl } from '@angular/forms';

@Component({
  selector: 'app-input',
  templateUrl: './input.component.html',
  styleUrls: ['./input.component.css'],
})

export class InputComponent implements OnInit {
  @Input() label: string;
  @Input() control: FormControl;
  constructor() {}

.......

这是我的输入模板文件:

<label>{{ label }}</label>
  <input type="text" [formControl]="control" />

【问题讨论】:

    标签: angular angular-reactive-forms reactive-forms


    【解决方案1】:

    如果您正在寻找可重用的表单组件,请查找 Angular ControlValueAccessor。你需要使用这个接口来创建一个可重用的表单组件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      • 1970-01-01
      • 2019-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多