2017-02-15 226 views
0

爲什麼我得到這個錯誤無法讀取未定義的被觸摸的財產?無法讀取屬性undefined

爲什麼不能讀取formName.controls['email'].touched但它能夠讀取formName.get('custDetails').touched

<form [formGroup]="formName"> 
<fieldset formGroupName="custdetails"> 
    <div class="form-group" [ngClass]="{'has-error': formName.controls['email'].touched 
       && formName.controls['email'].hasError('invalidEmailAddress')}"> 
     <label class="control-label">Email</label> 
     <input type="text" class="form-control" 
       formControlName="email" name="email" required /> 
    </div> 
</fieldset>   

</form> 

當我們使用formName.get('custdetails.email').touched ......我得到下面的埃羅

TypeError: _this.subscribe is not a function at eval (http://localhost:3000/node_modules/rxjs/operator/toPromise.js:68:15) at new ZoneAwarePromise (http://localhost:3000/node_modules/zone.js/dist/zone.js:551:29) at Object.toPromise (http://localhost:3000/node_modules/rxjs/operator/toPromise.js:66:12) at _convertToPromise (http://localhost:3000/node_modules/@angular/forms//bundles/forms.umd.js:541:73) at Array.map (native) at FormControl.eval [as asyncValidator] (http://localhost:3000/node_modules/@angular/forms//bundles/forms.umd.js:530:101) at FormControl.AbstractControl._runAsyncValidator (http://localhost:3000/node_modules/@angular/forms//bundles/forms.umd.js:2670:62) at FormControl.AbstractControl.updateValueAndValidity (http://localhost:3000/node_modules/@angular/forms//bundles/forms.umd.js:2630:26) at FormControl.setValue (http://localhost:3000/node_modules/@angular/forms//bundles/forms.umd.js:2988:18) at DefaultValueAccessor.eval [as onChange] (http://localhost:3000/node_modules/@angular/forms//bundles/forms.umd.js:1658:21) at Wrapper_DefaultValueAccessor.handleEvent (/InternalFormsSharedModule/DefaultValueAccessor/wrapper.ngfactory.js:29:34) at CompiledTemplate.proxyViewClass.View_ReactiveFormComponentFive0.handleEvent_36 (/AppModule/ReactiveFormComponentFive/component.ngfactory.js:717:45) at CompiledTemplate.proxyViewClass.eval (http://localhost:3000/node_modules/@angular/core//bundles/core.umd.js:12397:41) at HTMLInputElement.eval (http://localhost:3000/node_modules/@angular/platform-browser//bundles/platform-browser.umd.js:3224:57) at ZoneDelegate.invokeTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:275:35)

下面是如何我的形式是構建:

ngOnInit() { 
    this.formName = this.fb.group({ 
     name: ["", [Validators.required]], 
     custdetails: this.fb.group({ 
      email: ["", Validators.required, ValidationHelper.emailValidator], 
      confirm: ["", Validators.required] 
     }, { 
      validator: ValidationHelper.emailMatcher 
     }) 

    }) 
} 

而我的電子郵件valida TOR:

static emailValidator = (control: AbstractControl) : {[key: string]: boolean} =>{ 
     // RFC 2822 compliant regex 
     if (control.value.match(/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/)) { 
      return null; 
     } else { 
      return { 'invalidEmailAddress': true }; 
     } 
    } 

回答

3

到外地的完整路徑是:

formName.get('custdetails.email') 

所以,你需要訪問:

formName.get('custdetails.email').touched 

此外,你有一個錯誤的表單模型。當多個校驗器被施加到相同的字段,它們應的陣列內被包裝:

// Replace that: 
email: ["", Validators.required, ValidationHelper.emailValidator] 
// With this: 
// Notice the additional [ ] around the validators 
email: ["", [Validators.required, ValidationHelper.emailValidator]] 

通過傳遞兩個驗證作爲單獨的參數,角解釋第二確認emailValidator作爲異步驗證和它試圖訂閱它。因此錯誤消息「_this.subscribe不是一個函數」。

+0

我得到一個錯誤.... TypeError:_this.subscribe不是一個函數...檢查我更新的文章 – Shane

+0

也許問題出在表單模型聲明。你能否顯示錶單**模型**的代碼(即你在班上有什麼)? – AngularChef

+0

已更新帖子.... – Shane

相關問題