0

I am working on a Register module where User can add multiple channels, Like google, yahoo, outlook and custom.

Questions:-

  1. OnSelection of Custom Url field would be empty and in case of other domain url field would pre filled.
  2. If you select custom in domains then URl field needs to be blank and editable.
  3. If I click on add more then form needs to be blank same like when page is getting load.

I have created Dummy form at https://angular-db4rzu.stackblitz.io. Looking for help.

2
  • On point 3, I am referring form for added column. Commented Dec 11, 2018 at 19:09
  • code is available @ stackblitz.com/edit/angular-db4rzu You can fork and give a try. Thanks for quick reach out Commented Dec 11, 2018 at 20:54

1 Answer 1

1

You have a serious loop occurring in your project with the way you are subscribing to form changes, and then updating FormControl values based on the emit.

Essentially, you would change the value in the form, it would trigger scmTypeCheck() via the valueChanges subscription, which would set new values, then emit another change via valueChanges and trigger scmTypeCheck() again and again and again...

Then clicking Add More Row made it worse because the loop is growing exponentially, eventually exceeding the call stack limits.

In my Stackblitz example I replaced your valueChanges subscription with selectionChange event on your mat-select... this ensures there is no loop and your FormControl values are only updated once.

I also replaced your entire scmTypeCheck() logic with the following.

setDomainName(i, value) {
  //get the index of the selected value in the scmDummyStructure to use for default values
  const index = this.scmDummyStructure.findIndex(x => x.type === value);
  //get the url control to set state and default on user change
  const urlControl = this.scmForm.get('scmDatas')['controls'][i].get('url');

  //set default value
  urlControl.setValue(this.scmDummyStructure[index].url);

  //if custom type enable and clear any existing value if field not pristine else disable
  if (value === 'custom') {
    if (!urlControl.pristine && urlControl.value != '') {
      urlControl.setValue('');
      urlControl.enable();
    }else{
      urlControl.enable();
    }
  } else {
    urlControl.disable();
  }
}

Stackblitz

https://stackblitz.com/edit/angular-b7bjj6?embed=1&file=src/app/app.component.ts

Sign up to request clarification or add additional context in comments.

1 Comment

Please see revised stackblitz, specifically this line console.log(this.scmForm.getRawValue()) in your onSubmit()... you need to use getRawValue() to get the values of the disabled FormControl(s)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.