【问题标题】:EXCEPTION: Uncaught (in promise): TypeError: Cannot read property 'touched' of null例外:未捕获(承诺中):TypeError:无法读取属性'touched' of null
【发布时间】:2017-03-19 09:34:47
【问题描述】:

我正在尝试在我的 Angular2 应用程序中使用反应式表单,但我遇到了如下异常

异常:未捕获(承诺中):TypeError:无法读取属性 'touched' of null TypeError:无法读取属性'touched' of null

我的示例代码如下

import { Component, OnInit } from '@angular/core';
import { NgForm } from '@angular/forms';
import {FormGroup, FormControl} from '@angular/forms';
import { Customer } from './customer';

@Component({
moduleId:module.id,
templateUrl: 'sign-up.reactiveForms.html'
})

export class SignupComponent  {

customer: Customer= new Customer();


customerForm : FormGroup; //it assosciate html element with this root model

ngOnInit() : void {


   this.customerForm=new FormGroup({

        firstName: new FormControl(),
         lastName: new FormControl(),
         email: new FormControl(),
         sendCatalog: new FormControl(true)

   });
}

save() {
    console.log(this.customerForm);
  }

}

sign-up.reactiveForms.html 文件如下

            <!-- First Name input -->

            <div class="form-group"
                [ngClass]="{'has-error': (customerForm.get('firstName').touched ||customerForm.get('firstName').dirty) && !customerForm.get('firstName').valid }">
                <label class="col-md-2 control-label" 
                       for="firstNameId">First Name</label>

                <div class="col-md-8">
                    <input class="form-control" 
                           id="firstNameId" 
                           type="text" 
                           placeholder="First Name (required)" 
                           required 
                           minlength="3"
                           formControlName="firstName"
                             />
                    <span class="help-block" *ngIf="(customerForm.get('firstName').touched || customerForm.get('firstName').dirty) && customerForm.get('firstName').errors">
                        <span *ngIf="customerForm.get('firstName').errors.required">
                            Please enter your first name.
                        </span>
                        <span *ngIf="customerForm.get('firstName').errors.minlength">
                            The first name must be longer than 3 characters.
                        </span>
                    </span>
                </div>
            </div>

我试图弄清楚为什么 touched 属性为空。

【问题讨论】:

  • 该消息并不意味着所触摸的属性为空,这意味着 customerForm 没有名字。
  • 试过你的代码,我无法重现你的问题,这意味着它对我来说很好。作为一个疯狂的猜测,尝试使用安全导航运算符。至少这应该处理空错误,但也许验证将不起作用(?)。但是尝试...例如:customerForm.get('firstName')?.touched

标签: angular reactive-forms


【解决方案1】:

尝试并初始化您的表单控件,使其不再是null

this.customerForm=new FormGroup({
    firstName: new FormControl(''),
    lastName: new FormControl(''),
    email: new FormControl(''),
    sendCatalog: new FormControl(true)
});

否则,使用安全导航运算符可能会删除 null 错误,例如:

customerForm.get('firstName')?.touched

但是单独初始化表单控件应该(希望)解决这个问题。

【讨论】:

    猜你喜欢
    • 2018-04-18
    • 1970-01-01
    • 2017-12-01
    • 2017-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多