1

I am trying to change my formarray values based on the index as soon as a change in the form occurs. The following situation is given: I have given phases each with a start and end date. Except for the first phase, the start date of a phase should always be the same as the end date of the previous phase.

I think the error occurs because the valueChanges always calls itself as the value of the form is changed at the end of the method.

  onChanges() {
this.projectForm.controls['phases'].valueChanges.subscribe(value => {
  for (var i = 1; i <= this.phasecount; i++) {
      this.projectPhases.at(i).patchValue({ pStart: value[i - 1].pEnd });                   
  }
})

}

do any of you have an idea how to work around the error (maybe a termination condition)?

Thanks in advance.

3
  • How often is onChanges called here..? Just once I hope? Commented Dec 18, 2020 at 10:58
  • Hey, only once in the NgOnInit-function Commented Dec 18, 2020 at 11:00
  • You can try to add {emitEvent: false, onlySelf:true} as options for the patchValue Commented Dec 18, 2020 at 11:00

1 Answer 1

3

This line this.projectPhases.at(i).patchValue({ pStart: value[i - 1].pEnd }); triggers this event this.projectForm.controls['phases'].valueChanges and it infinite loops.

You could set emitEvent option to false while doing patchValue :

this.projectPhases.at(i)
.patchValue({ pStart: value[i - 1].pEnd }, { emitEvent: false });
Sign up to request clarification or add additional context in comments.

3 Comments

thanks alot, no maximum call stack size anymore :). But im now getting the following error: TypeError: Cannot read property 'patchValue' of undefined.
This is probably because somehow you get into a state where this.projectPhases.at(i) is undefined, you should probably debug this line of code until you get into this case.
nevermind, i had to put < into the for-loop not <=

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.