【问题标题】:reactive form is not able to identify username, email, course反应形式无法识别用户名、电子邮件、课程
【发布时间】:2020-06-08 00:17:22
【问题描述】:

在反应式表单中,错误是无法识别用户名、电子邮件和课程字段。

  1. 在 ts 文件中我创建了 formGroup
  2. 在表单控制中,我创建了 3 个属性用户名、电子邮件、课程
  3. 在 HTML 文件中,我在 formControl 中绑定了同名

        <form [formGroup]="myReactiveForm">
    
            <div >
                <label>User Name</label>
                <input type="text" [formControl]="username">
            </div>
    
    
            <div >
                <label>Email</label>
                <input type="text" [formControl]="email">
            </div>
    
    
            <div>
                <select name="course" [formControl]="course">
                    <option value="Angular">Angular</option>
                    <option value="">Html</option>
                </select>
            </div>
    
    
            <button>Submit</button>
        </form>
    
    </div>
    


    import { Component, OnInit } from '@angular/core';
    import { FormGroup, FormControl } from '@angular/forms';
    
    @Component({
      selector: 'app-comp',
      templateUrl: './comp.component.html',
      styleUrls: ['./comp.component.css']
    })
    export class CompComponent implements OnInit {
    
      myReactiveForm : FormGroup;
    
    
    
      constructor() { }
    
          ngOnInit(): void {
    
            this.myReactiveForm = new FormGroup({
    
              'username' : new FormControl(null),
              'email' : new FormControl(null),
              'course' : new FormControl(null)
            });
          }
    
        }
    

错误是:

`error TS2339: Property 'username' does not exist on type 'CompComponent'`

    8             <input type="text" [formControl]="username">

【问题讨论】:

    标签: angularjs typescript


    【解决方案1】:

    你必须使用formControlName,比如&lt;input type="text" formControlName="email"&gt;,因为formControl是你直接使用表单控件的时候,而不是表单组。

    【讨论】:

      猜你喜欢
      • 2021-12-21
      • 1970-01-01
      • 2021-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 2021-03-04
      • 2010-09-28
      相关资源
      最近更新 更多