简体   繁体   English

Angular FormControl 值设置为 null 内部服务

[英]Angular FormControl value set to null inside service

I have a Reactive Angular form which takes mobile number and some other fields as inputs.我有一个反应式 Angular 表单,它以手机号码和其他一些字段作为输入。 Below is the code:下面是代码:

component.html组件.html

<form [formGroup]="contactDetailsForm">
  <ngx-intl-tel-input [cssClass]="'ngxIntlInputBorder'" [preferredCountries]="preferredCountries"
          [enableAutoCountrySelect]="false" [enablePlaceholder]="false" [searchCountryFlag]="true"
          [searchCountryField]="[SearchCountryField.Iso2, SearchCountryField.Name]" [selectFirstCountry]="false"
          [selectedCountryISO]="countryCode" [maxLength]="10" [tooltipField]="TooltipLabel.Name"
          [phoneValidation]="false" aria-label="pMobileNo" formControlName="primaryMobile" id="phone"
          (focusout)="validatePrimaryMobile()"></ngx-intl-tel-input>
</form>

component.ts组件.ts

ngOnInit() {
    this.serviceForm = this.registerService.getContactDetails();
    if(this.serviceForm!=null){
      this.contactDetailsForm = this.serviceForm;
      if(this.serviceForm.get('country').value != 91){
        this.indiaFlag = false;
      }
      return;
    }
    this.contactDetailsForm = new FormGroup({
      primaryMobile: new FormControl('', [Validators.required, Validators.minLength(10)]),
    });
  }

ngOnDestroy(){
    this.registerService.holdContactDetails(this.contactDetailsForm);
  }

component.service组件服务

holdContactDetails(contactDetails:FormGroup){
    this.contactDetails=contactDetails;
    console.log('Hold: ', this.contactDetails);
    this.mobile = this.contactDetails.get('primaryMobile').value;
    console.log("Mobil obj: ", this.mobile);
  }

  getContactDetails():FormGroup{
    if(this.contactDetails!=null){
      console.log('this.mobile: ', this.mobile);
      this.contactDetails.controls.primaryMobile.value = this.mobile;
      console.log('get: ', this.contactDetails)
    }
    return this.contactDetails;
  }

When calling the holdContactDetails() , the value of formGroup is updated successfully and i can see the below value of this.mobile on console:调用holdContactDetails()时,formGroup 的值已成功更新,我可以在控制台上看到this.mobile的以下值:

{number: "9958244118", internationalNumber: "+91 99582 44118", nationalNumber: "099582 44118", countryCode: "IN", dialCode: "+91"}

When calling the getContactDetails() function, the value of this.mobile is correctly printed inside the function but in the returned formGroup, the value for primaryMobile is null.调用getContactDetails() function 时, this.mobile的值正确打印在 function 中,但在返回的 formGroup 中, primaryMobile的值为 Z37A6259CC0C1DAE299A7866498。 Below is the screenshot of the same:以下是相同的屏幕截图:

调用holdContactDetails()

调用 getContactDetails()

As you can see in the images, when holding, the value for mobile is an object and status is also valid.正如您在图像中看到的,当持有时,mobile 的值为 object 并且状态也是有效的。 But on calling get function, the value is updated to null and statu to invalid.但是在调用 get function 时,该值更新为 null 并且状态无效。 Why is this happening?为什么会这样?

Update I think I need to be more clear regarding my question.更新我认为我需要更清楚地了解我的问题。 The question here is not how to set the form control value inside service.这里的问题不是如何在服务中设置表单控件值。 My question is why do I need to set the value of a form control explicitly when it is already set when I send the formGroup to the service.我的问题是,当我将 formGroup 发送到服务时,为什么我需要显式设置表单控件的值。

Reactive forms are sometimes confusing, the thing is you can not set a value to them using the assign operator (=) and you need to use the two methods from Angular Formgroup: setValue and patchValue The difference between these two is that setValue is for the whole formGroup no matter how many controllers that group has, but in the other hand, patchValue way easier to use.反应式 forms 有时会令人困惑,问题是您不能使用赋值运算符 (=) 为它们设置值,您需要使用 Angular Formgroup 中的两种方法: setValuepatchValue这两者之间的区别在于 setValue 用于整个 formGroup 不管该组有多少个控制器,但另一方面,patchValue 方式更易于使用。 Example of using patchValue:使用 patchValue 的示例:

this.contactDetails.get('primaryMobile').pathValue('123456789');

you can learn more about reactive forms in Angular here: https://link.medium.com/TRvHAhuzB7您可以在此处了解有关 Angular 中反应性 forms 的更多信息: https://link.medium.com/TRvHAhuzB7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM