stupidawesome / ng-effects

Reactivity system for Angular. https://ngfx.io
MIT License
46 stars 3 forks source link

Accessing lazy loaded ViewChildren #12

Closed st-clair-clarke closed 3 years ago

st-clair-clarke commented 4 years ago

I have this peculiar issue that involves angular Material Expansion panel.

path.component.html

<mat-accordion
        [multi] = 'multi'
    >
      <mat-expansion-panel
          [expanded] = 'false'
          class = 'mat-elevation-z7'
          hideToggle = 'false'
      >
        <ng-template matExpansionPanelContent>
          <nhncd-dx-tree-select-search
              [dataSource$] = 'glucose$'
              placeholder = 'Glucose'
              target = 'Endocrinology:Glucose'></nhncd-dx-tree-select-search>
        </ng-template>
      </mat-expansion-panel>

      <mat-expansion-panel
          class = 'mat-elevation-z7'
          hideToggle = 'false'
      >
        <ng-template matExpansionPanelContent>
          <nhncd-dx-tree-select-search
              [dataSource$] = 'ogtt$'
              placeholder = 'OGTT'
              target = 'Endocrinology:OGTT'></nhncd-dx-tree-select-search>
        </ng-template>
      </mat-expansion-panel>

path.component.ts

class....

  @ViewChildren(TemplateRef) queryList: QueryList<
    TemplateRef<NhncdDxTreeSelectSearchComponent>
  >

  ngAfterViewInit(): void {
    console.log('list length', this.queryList.length)

    this.queryList.changes.subscribe((list) => {
      list.forEach((instance) => console.log('target is: ', instance.target))
    })
  }
}

end path.component.ts

The issue is that by placing the inside the it becomes lazy - it only is instantiated when the expansion panel is opened! How can I access the instantiated using ViewChildren and ng-effects - afterall, ngAfterViewInit has already run when the panel is opened (instantiated)

stupidawesome commented 4 years ago

Not quite sure what's going on here, but it doesn't look like it's actually using any of this library's code. The changes observable doesn't fire during ngAfterViewInit, so maybe you just need to add a startsWith operator.

this.queryList.changes.pipe(startsWith(this.queryList)).subscribe((list) => {
  list.forEach((instance) => console.log('target is: ', instance.target))
})
st-clair-clarke commented 4 years ago

Thanks