0

I tried to get index of child component in ParentComponent. My structure of ParentComponent was

<mat-toolbar>
  <ng-container #buttonContainer>
    <chameleon-ng-button [config]="buttonAdd"></chameleon-ng-button>
  </ng-container>
</mat-toolbar>

<ng-template #tmpButton let-config>
  <chameleon-ng-button [config]="config"></chameleon-ng-button>
</ng-template>

I init buttonContainer like this

@ViewChild('buttonContainer', { read: ViewContainerRef, static: true })
protected buttonContainer: ViewContainerRef;

With this, I hope I could have default component (buttonAdd) and could load other button dynamicly using

const buttonTmp = this.tmpButton.createEmbeddedView({
   $implicit: config,
});

this.buttonContainer.insert(buttonTmp);

After seeks for a while, I still doesn't understand why I couldn't get index of my ButtonAdd by this way this.buttonContainer.length

I got empty array instead of 1. Any answer will be appreciate :)

0 Answers0