在FormControl中,当我尝试获取任何元素时都变得不确定,但是当我尝试在手表中查找时,我得到了该表单控件元素

时间:2019-03-01 10:30:48

标签: javascript angular typescript angular6

请找到该问题的附件图片。

在我的控制台中,您可以找到未定义的值,但是同样,我尝试观察一下,在那里我得到了FormControl的值。

我不明白为什么会发生

image

private _customerForm: NgForm;
In a constructor
this._subscriptions.push(
            _customerDetailsFeatureService.customerForm$.subscribe(form => {
                this._customerForm = form;
            })
        )
public get isValidCustomerFormForDraft() {
        let isValidEmailAndShortName = (shortNameCtr: AbstractControl, emailCtr: AbstractControl ,phoneCtr : AbstractControl) => {
            return (shortNameCtr && !shortNameCtr.hasError('pattern')) &&
                   (emailCtr && !emailCtr.hasError('validEmail')) && 
                   (phoneCtr  && !phoneCtr.hasError('pattern'))
        }

        if (this.allInfoCustomer && this.allInfoCustomer.customer && this.allInfoCustomer.customer) {
            if (!this._customerForm || !this._customerForm.control) return true;

            switch (this.allInfoCustomer.customer.customerType) {
                case CustomerTypeForCountry.Legal: {
                    let shortNameCtr = this._customerForm.control.get('legalShortName');
                    let emailCtr = this._customerForm.control.get('legalEmail');
                    let phoneCtr = this._customerForm.control.get('phone');

                    var value = this.allInfoCustomer.customer.companyName &&
                           this.allInfoCustomer.customer.shortNameValid &&
                           this.allInfoCustomer.customer.customerGroupId &&
                           isValidEmailAndShortName(shortNameCtr, emailCtr,phoneCtr)

                           var ddd =  isValidEmailAndShortName(shortNameCtr, emailCtr,phoneCtr);

                           return value;
                }
                case CustomerTypeForCountry.Individual:
                let shortNameCtr = this._customerForm.control.get('shortName');
                let emailCtr = this._customerForm.control.get('email');
                let phoneCtr = this._customerForm.control.get('phone');

                    return this.allInfoCustomer.customer.firstName &&
                           this.allInfoCustomer.customer.lastName &&
                           this.allInfoCustomer.customer.shortNameValid &&
                           isValidEmailAndShortName(shortNameCtr, emailCtr,phoneCtr)
            }
        }
        return false;
    }

1 个答案:

答案 0 :(得分:0)

从表单获取值时,应使用controls而不是表单中的控件,这就是为什么要获取未定义值的原因

要获得控件的价值,您应该这样做

let emailCtr = this._customerForm.value.email;

否则您可以执行此操作

let emailCtr = this._customerForm.controls['email'].value;