0

I have a page a component that displays a list of items. When a user clicks on the item, a form should be displayed with the relevant data. Users also have the option of creating new items by selecting the relevant form.

I have the following in the parent component:

<ng-container [ngSwitch]="type">
          <ng-container *ngSwitchCase="'TYPE1'">
            <page-type-A></page-type-A>
          </ng-container>
          <ng-container *ngSwitchCase="'TYPE2'">
            <page-type-B></page-type-B>
          </ng-container>

The problem I am facing is highlighted by the following user experience:

  1. They create a new item (call it item 3 of item type-A)
  2. They save the item and are navigated back to the "item overview page"
  3. They select a different item (item 1) of item type-A
  4. They are shown the form with the data of item 1

The issue: is that the component is never destroyed. So when they went to create item 3, the component was initialized once, but then never again. This means that I am unable to use angulars lifecycle hooks. I do not know angular very well, and this is something that keeps popping up.

I am wondering if there is something obvious that I can do to fix this issue. I really want the component destroyed everytime it is closed, so that next time that item-type is called, the ngOnInit() gets called.

I believe that if I was using an *ngIf instead of the switch, but in this case it's just cleaner to use a switch.

Richard
  • 121
  • 8
  • Please produce a minimal stackblitz for debugging purposes. – El-Mo Mar 14 '21 at 22:21
  • So resolved the issue.. It's probably obvious to many, and without the stackblitz it's difficult to get value from this but thought I'd share anyway. We were using the switch statement, and the new "switch member" was only selected later down the process (e.g. we only selected TypeB later). So ngOnDestroy was only being called further down the path than expected. Lesson learned: important to really understand where/when the angular hooks are being called.. – Richard Mar 15 '21 at 14:48

0 Answers0