Call a parent component function from a child component in Angular

LLaza

I am trying to call my close() function which is in the parent component from the child component in Angular.

Here is my code on Plunker, I tried to import the component and call the function directly in my child component but I get this error: Cannot read property 'close' of undefined

How can I fix this?

Hamed Baatour

I will try to give a complete answer to both cases that you may encounter:

Case 1: Calling a child function from the parent component

You can achieve this simply by using a template variable on your child selector to reference your child component in your parent component template and then you can call any public functions or property using that reference.

so in your child component, you got a function:

test(){
    console.log(`this is a test`);
}

and in your parent component you can call it in your parent component let's say after a button click just like this:

<child-component #childRef></child-component>
<button (click)="childRef.test()">
    Call the child test function
</button>

Case 2: Calling a parent function from the child component

This is a case it's up to your use case as you can inject the parent component in the child component and do the same as above but the relation is no longer be parent-child or child-parent but both components will be strongly linked together which is a rare use case. Alternatively, you can get the same result in a very simple way by using the @Output decorator to pass data and create your function in your child that will consume the data passed in.

so in your child component, you do the following:

@Output() childEvent = new EventEmitter();
test(){
    this.childEvent.emit('this is a test');
}

in your parent template

<child-component (childEvent)="test($event)"></child-component>

and then in your component

test(msg){
    console.log(msg);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Call a child component function from parent component in Angular 1.5

angular 4 - call child component function from parent component html

Call a parent component function from a child component

Call child component function from parent

Angular call parent component function from child component, update variable in real time from sessionStorage

angular router how to call child component from the parent component

Unable to call parent component function from child component in React

How to have a React child component call a function from a parent component

How to call React/Typescript child component function from parent component?

Angular 2+ call a function in a child component from its parent component

How to access function from child component to parent component in angular

Call parent component function from child when child components are in router outlet Angular

Executing child (component) function from the parent component

Parent component to call child component function on a list of children component

How to call parent controller function from within a child component in Angular 1.6.7

Call child component method from parent class - Angular

Call a function in imported child component on click from a parent element?

Unable to call child function from parent using refs with functional component

How can I call Child function from Parent Component in React

Angular 4 call parent method in a child component

How to call function on child component on parent events

Call function in child component only after function in parent component is completed

Call method from parent to child via component

Call a function in a child component based on an input from user in Angular

Using a button in parent component to call function in child component

How can I call the function of a parent component inside of a child component?

How to Call child function from parent component in React Native functional component?

How to call function in parent class component by onchange event from child component in React?

How to call an event in parent component from child component?