5

Hi Tôi đang thực hiện một hình thức trong góc 2 sử dụng Form BuilderForm Builder với hasError() để xác nhận ném một lỗi của ERROR Lỗi Loại: Không thể đọc thuộc 'hasError' không xác định

trong component.ts tôi đã thực hiện của tôi hình thức sử dụng formGroup

Dưới đây là mã của tôi

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: [''], 

    }); 

} 

Nó đang làm việc tốt. Nhưng khi đến để hiển thị kiểm chứng thực trong frontend

Tôi đã từng như thế này

<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> 

nó đang làm việc nhưng ném lỗi trong giao diện điều khiển như Dưới

ERROR Lỗi Loại: Không thể đọc thuộc 'hasError' không xác định

Vui lòng giúp tôi cách sắp xếp điều này.

Cảm ơn bạn.

Trả lời

9

Bạn nên sử dụng nó như thế này:

<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> 
+0

Các giá trị tùy chọn là những gì giúp tôi. Cách tiếp cận của tôi hơi khác một chút, tôi sử dụng điều khiển? .hasError ("someError"). Cảm ơn bạn! – kbpontius

Các vấn đề liên quan