无法读取未定义的属性“触及”

时间:2017-02-15 12:42:57

标签: javascript angular typescript

为什么我收到此错误无法读取未定义的属性

为什么它无法阅读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不是函数       在eval(http://localhost:3000/node_modules/rxjs/operator/toPromise.js:68:15)       在新的ZoneAwarePromise(http://localhost:3000/node_modules/zone.js/dist/zone.js:551:29)       在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)       在Array.map(本机)       在FormControl.eval [as asyncValidator](http://localhost:3000/node_modules/@angular/forms//bundles/forms.umd.js:530:101)       在FormControl.AbstractControl._runAsyncValidator(http://localhost:3000/node_modules/@angular/forms//bundles/forms.umd.js:2670:62)       在FormControl.AbstractControl.updateValueAndValidity(http://localhost:3000/node_modules/@angular/forms//bundles/forms.umd.js:2630:26)       在FormControl.setValue(http://localhost:3000/node_modules/@angular/forms//bundles/forms.umd.js:2988:18)       在DefaultValueAccessor.eval [as onChange](http://localhost:3000/node_modules/@angular/forms//bundles/forms.umd.js:1658:21)       在Wrapper_DefaultValueAccessor.handleEvent(/InternalFormsSharedModule/DefaultValueAccessor/wrapper.ngfactory.js:29:34)       在CompiledTemplate.proxyViewClass.View_ReactiveFormComponentFive0.handleEvent_36   (/AppModule/ReactiveFormComponentFive/component.ngfactory.js:717:45)       在CompiledTemplate.proxyViewClass.eval(http://localhost:3000/node_modules/@angular/core//bundles/core.umd.js:12397:41)       在HTMLInputElement.eval(http://localhost:3000/node_modules/@angular/platform-browser//bundles/platform-browser.umd.js:3224:57)       在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
        })

    })
}

我的电子邮件验证员:

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 个答案:

答案 0 :(得分:5)

该字段的完整路径是:

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

通过将两个验证器作为单独的参数传递,Angular将第二个验证器emailValidator解释为异步验证器,并尝试订阅它。因此,错误消息&#34; _this.subscribe不是函数&#34;。

答案 1 :(得分:0)

添加TS文件

*ngIf="email.touched"

在html中添加验证

while not EventStopAll.isSet(): time.sleep(self.cpu_relieve_time) if self.__new_detection_event(DetectionEvent): # do something if EventStopMeasuring.isSet() and self.detectionEvent_active: # do something else

答案 2 :(得分:-1)

你需要在这个输入中写入ngClass feild错误将被解决。