1

I have parent component id="componentOne" where is included another child component <app-buttons>:

<div id="componentOne">
    <app-buttons
    [component]="'WeeklyScheduleComponent'"
    [buttonTypes]="['add', 'filter']"
    [position]="'right-bottom'"
  ></app-buttons>
</div>

Inside component app-buttons there is:

 ngOnInit() {
    this.buttonUsage = new ButtonUsage(this.component, this.buttonTypes);
  }

So, how to get access to this.buttonUsage from parent component after initialization this.buttonUsage = new ButtonUsage?

POV
  • 11,293
  • 34
  • 107
  • 201

1 Answers1

1

You can access using the viewChild and your component instance will be available in ngAfterViewInit

@ViewChild(AppButtonsComponent) appButtonsViewChild: AppButtonsComponent;


ngAfterViewInit() {
 console.log(this.appButtonsViewChild.buttonUsage );
}

if you have multiple children you can use @ViewChildren decorator along side the QueryList generic type

@ViewChildren(AppButtonsComponent) appButtonsViewChildren: QueryList<AppButtonsComponent>;

ngAfterViewInit() { 
  let buttons: AppButtonsComponent[] = this.appButtonsViewChildren.toArray();
  console.log(buttons);
}
Fateh Mohamed
  • 20,445
  • 5
  • 43
  • 52
  • 1
    Since @angular 8: @ViewChild(AppButtonsComponent, {static: false}), or {static:true}, depending on when you want your component to be available at your typescript. – julianobrasil Jul 01 '19 at 08:59