Home > Blockchain >  Angular how to keep formgroup data in component after hiding it with ngIf (destroying it)
Angular how to keep formgroup data in component after hiding it with ngIf (destroying it)

Time:01-12

I have an Angular app where I have this problem at multiple places, for example:
There's a search component, and another filtering component for the search, I show / hide the filter component with an ngIf, and in it's ngOnInit() I create a formGroup for the filter values.

The problem is that after I use the filter, and close this component (the ngIf changes to false), and then when I want to use it again, I change the value controlling the ngIf to true, and the filter component makes a new empty form.

I want to keep the values there somehow, so that what I wrote there stays there.

CodePudding user response:

I think the solution for it is to initial the FormControl outside the component - or in some service and then you can get it via dependency injection, or to initial it in the parent component and pass it to the child component via @Input()

good luck :)

  •  Tags:  
  • Related