5

のプロパティを読み取ることができません「hasError」こんにちは、私は私は私を実装しているcomponent.tsにフォームビルダフォームビルダ():未定義

を使用して、角度2でフォームを実装しています以下は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: [''], 

    }); 

} 

です。未定義のプロパティ「hasError」を読み取ることができません:フロントエンドでの検証を表示するために来たときにでも

私はそれが動作しますが、エラーの例外TypeErrorが

以下のようにコンソールにエラーを投げている。この

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

のように使用しました

どうすればこの問題を解決するのか教えてください。

ありがとうございます。

答えて

9

あなたはこのようにそれを使用する必要があります。

<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

オプションの値が私を助けたものです。私のアプローチは少し違って、私はコントロールを使用していますか?.hasError( "someError")。ありがとうございました! – kbpontius