5
你好我在執行中component.ts使用表單生成表單生成與hasError()進行驗證拋出錯誤類型錯誤的錯誤:無法讀取未定義
在角2形式我已經實現的特性「hasError」我使用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: ['', [<any>Validators.required]],
city: ['', [<any>Validators.required]],
company_address: ['', [<any>Validators.required]],
company_address2: ['', [<any>Validators.required]],
company_city: ['', [<any>Validators.required]],
company_country: ['', [<any>Validators.required]],
company: ['', [<any>Validators.required , Validators.minLength(3)] ],
company_tax_number: ['', [<any>Validators.required]],
company_zip: ['', [<any>Validators.required, Validators.minLength(5) , Validators.maxLength(7)]],
country: ['', [<any>Validators.required]],
email: ['', [<any>Validators.required, Validators.email]],
first_name: [this.userDetails.first_name, [<any>Validators.required]],
id: ['', [<any>Validators.required]],
last_name: ['', [<any>Validators.required]],
phone: ['', [<any>Validators.required, Validators.minLength(10)]],
zip: ['', [<any>Validators.required , Validators.minLength(5) , Validators.maxLength(7)]],
user_type: ['2', [<any>Validators.required]],
terms: [0, [<any>Validators.required]],
hash_tag: [''],
});
}
這是工作的罰款。但未來在前端顯示驗證時
我用這樣的
<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>
這是工作,但引發錯誤控制檯類似下面
錯誤類型錯誤:無法讀取的未定義的屬性「hasError」
請幫我一下如何排序。
謝謝。
可選的值是什麼幫助了我。我的方法有點不同,我使用control?.hasError(「someError」)。謝謝! – kbpontius