-1

I am using Angular 5. I want to call the child component method from parent component. For this I am adding the import {myChildComponent} from './myChild/myChild.component'; //ERROR: Cannot find module ./myChild/myChild.component

How can I import the child component in any other component other than app.component.ts.(here it works)

user2480755
  • 97
  • 3
  • 11

1 Answers1

0

You can simply call function of child component from parent component using local variable in Angular.

For example

<hello name="{{ name }}" #childOne></hello>
<p (click)='childOne.callFromParent()'>
  Start editing to see some magic happen :)
</p>

here hello is child component.

Working Example

Second way

<hello name="{{ name }}" #childOne></hello>
<button (click)='callChildFunction()'>hello</button>

@ViewChild('childOne') childOne: any;

callChildFunction() {
    this.childOne.callFromParent();
  }
Community
  • 1
  • 1
Pardeep Jain
  • 84,110
  • 37
  • 165
  • 215
  • Nice! But I am not sure how it will work in my scenario. I have two components one is master with 2 buttons and one for map-view. When I click one of the buttons in master I need to pass the marker type for map. Based on which I can draw custom markers on map. I tried passing it as parameter but seems like the value is not received on child or map-view component. Thats why I tried to call the map-view method(which changes the map marker) from master component. – user2480755 Jun 07 '18 at 11:26
  • May `@input/@output` trick for you – Pardeep Jain Jun 07 '18 at 11:41