Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

361
Views
Angular - trying to use child component function in parent view but I'm gettting an error

When I use @ViewChild I get the error that the component is not defined. When I use @ViewChildren I get the error that the function from that component is not a function.

I am new to using child components in Angular so I'm not sure why it's doing this when I do have the child component defined in the parent component and when it's clearly a function in the child component.

I don't want to have to define every function from the child in the parent or else what's even the point of using a separate component.

Child Component

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-mood',
  templateUrl: './mood.component.html',
  styleUrls: ['./mood.component.css']
})
export class MoodComponent implements OnInit {

  moodColors = ['red', 'orange', 'grey', 'yellow', 'green'];

  constructor() { }

  ngOnInit(): void {
  }

  chooseMood() {
    alert(this.moodColors);
  }

}

Parent Component (Relavant Part of Version with "ERROR TypeError: ctx_r3.mood is undefined")

import { Component, OnInit, ViewChild, ViewChildren } from '@angular/core';
import { ViewEncapsulation } from '@angular/core';
import { MoodComponent } from '../mood/mood.component';

@Component({
  selector: 'app-calendar',
  templateUrl: './calendar.component.html',
  styleUrls: ['./calendar.component.css'],
  encapsulation: ViewEncapsulation.None
})
export class CalendarComponent implements OnInit {

  @ViewChild('mood') mood: MoodComponent = new MoodComponent;

Parent Component (Relavant Part of Version with "ERROR TypeError: ctx_r3.mood.chooseMood is not a function")

import { Component, OnInit, ViewChild, ViewChildren } from '@angular/core';
import { ViewEncapsulation } from '@angular/core';
import { MoodComponent } from '../mood/mood.component';

@Component({
  selector: 'app-calendar',
  templateUrl: './calendar.component.html',
  styleUrls: ['./calendar.component.css'],
  encapsulation: ViewEncapsulation.None
})
export class CalendarComponent implements OnInit {

  @ViewChildren('mood') mood: MoodComponent = new MoodComponent;

Parent View

<h2 (click)="mood.chooseMood()"></h2>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You don't explicitly initialize view children via new.

Just use:

@ViewChild('mood') mood : MoodComponent;

If that doesn't work post a Stackblitz example which I can edit to resolve the issue.

Also, using ViewChild is more of an exception in Angular, and your use of it points to a probable design issue. More likely you child component should emit via an Output to the parent.

Regarding outputs, you can do something like this - though it is hard to give a precise answer without deeper knowledge of what you are trying to achieve:

export class MoodComponent implements OnInit {
 @Input() moodId: string;
 @Output() chooseMood = new EventEmitter<string>();

  moodClicked(){
    this.chooseMood.emit(moodId);
  }
}

export class CalendarComponent implements OnInit {
  moodChosen(string: moodId){
    console.log(moodId);
  }
}

// Calendar template:

<app-mood
  moodId="happy"
  (chooseMood)="moodChosen($event)"
></app-mood>
about 4 years ago · Juan Pablo Isaza Report

0

1 - you have to use this code

@ViewChild('mood') mood : MoodComponent;

when you are using @ViewChildren it will return list of items with the 'mood' name then you have to use this code

mood.first.chooseMood() ;

its better use ViewChildren when there is ngIf in your element

2- no need new keyword for initialize mood variable it would be fill after ngOnInit life cycle fires

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!