user2004
user2004

Reputation: 1973

cannot call method from @viewchildren in parent

I am trying to call a method from child component in parent component because I have this html code for parent

 <div class="right view-calendar">
       <child *ngFor="let selectedMonth of selectedMonths" [viewDate]="selectedMonth" [monthList]="show()"></child>
 </div>

parent component:

export class ParentComponent implements OnInit {
      @ViewChildren(MonthHeaderComponent) months: any[];
        show() {
         this.months.method();
       }
    }

child component

export class ChildComponent implements OnInit {
   @Input() monthsList: Date[]
   method() {
   // code here....
   }
}

I've read this issue Can't get to @ViewChildren in parent component but I don't know if there's something that can help me.

What am I doing wrong here?

Upvotes: 0

Views: 1504

Answers (1)

user4676340
user4676340

Reputation:

You have the wrong types and are trying to call a method on on a list of children, not the child itself.

@ViewChildren(MonthHeaderComponent) months: QueryList<MonthHeaderComponent>;

I would suggest that you use a template variable to ease your understanding of the issue :

<child *ngFor="..." #children [viewDate]="selectedMonth" [monthList]="show(children)"></child>
show(child) {
  child.method();
}

Upvotes: 2

Related Questions