1

I am trying to populate the form field when click on edit icon. For the first time it is working as expected but after I click different edit icon it doesn't update. Also if I hit cancel button and again click any edit button it is working fine. But it only works for the first time.

Stackblitz demo

I am calling that patchValue thing inside the ngAfterViewInit.

  ngAfterViewInit(): void {
     setTimeout(() => {
       this.editDataToForm();  
     }, );
  }

It should update the form field whenever I click any of the edit icon.

1 Answer 1

1

The issue is that the ngAfterViewInit lifecycle hook only is proc'd once during the component's life (after the view is initialized), and the component isn't being destroyed except for when you completely close the form.

In order to update the form any time the form data is changed you will also need to hook into the ngOnChanges lifecycle hook.

  ngOnChanges(changes: SimpleChanges) {
    console.log('change detected: ', changes)
    setTimeout(() => {
      this.editDataToForm();  
    }, );
  }

Forked StackBlitz: https://stackblitz.com/edit/angular-t7jams

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

Comments

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.