0

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?

user2004
  • 1,783
  • 4
  • 15
  • 42
  • 1
    Well _tell us_ what happens. "I am trying" ... "I don't know if there's something that can help me" ... "What am I doing wrong". We can't read your mind,. – Lazar Ljubenović Dec 05 '18 at 12:10
  • @LazarLjubenović people with Angular skills will easily understand the issue. the OP can't find his error because of reasons, that's why he's askign for help. If he knew his issue, he wouldn't have posted a question :) –  Dec 05 '18 at 12:11
  • @trichetriche I _am_ a person "with Angular skills". I'm not saying that the OP should know the _reason_. I'm saying to at least post the error message if you want help. Is there even an error message? Does it swallow an error? What is expected? What is happening -- did your computer explode or what? It has nothing to do with Angular and everything to do with basics of asking for help. – Lazar Ljubenović Dec 05 '18 at 16:21
  • Sorry that wasn't directed at you, I'm author-agnostic, I tend to not see names or profiles unless I really need to. I agree with you, those are the basics, I won't contest that. But given the enormous time I spend here, and all the types of questions I see all day, I consider this question to be "answerable" and don't think more context is necessary. Here are my two cents on the matter, again sorry for seeming rude ! –  Dec 05 '18 at 17:39

1 Answers1

2

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();
}