嗨,我正在使用表单生成器在角度2中实现一个表单
在component.ts中,我使用formGroup实现了我的表单
以下是我的代码
public myForm: FormGroup; constructor(private authenticateservice: AuthenticateService,private _fb: FormBuilder ) { } ngOnInit() { this.myForm = this._fb.group({ address: [this.userDetails.address,[<any>Validators.required]],address2: ['',city: ['',company_address: ['',company_address2: ['',company_city: ['',company_country: ['',company: ['',[<any>Validators.required,Validators.minLength(3)] ],company_tax_number: ['',company_zip: ['',Validators.minLength(5),Validators.maxLength(7)]],country: ['',email: ['',Validators.email]],first_name: [this.userDetails.first_name,id: ['',last_name: ['',phone: ['',Validators.minLength(10)]],zip: ['',user_type: ['2',terms: [0,hash_tag: [''],}); }
它工作正常.但是在frontEnd中显示验证时
我这样用过
<div class="form-group row"> <div class="col-lg-8"> <label>Address 2</label> <textarea class="form-control" placeholder="Address" rows="2" [readonly]="disabled" id="companyaddress2" formControlName="company_address2"></textarea> <span class="help-block form-error text-danger small" *ngIf="myForm.controls['company_address2'].hasError('required')">Company Address 2 is Required.</span> </div> </div>
它正在工作,但在控制台中抛出错误,如下
错误TypeError:无法读取未定义的属性’hasError’
请帮我解释一下这个问题.
谢谢.
你应该像这样使用它:
<span class="help-block form-error text-danger small" *ngIf="myForm.controls['company_address2'].errors?.required && myForm.controls['company_address2'].touched">Company Address 2 is Required </span>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。