-1

My parent class code:

import { Child } from './child';

class Parent{
     // onGateOpen(para1); how to access it here ? 
}

My child class code is in separate file named child.ts:

class Child extends parent{
   onGateOpen(para1){
        // some code for opening the gate is here.
   }
}

Thanks.

Roland Rácz
  • 2,879
  • 3
  • 18
  • 44
uday214125
  • 569
  • 1
  • 8
  • 22
  • There is a lot of documentation on this on the [angular website](http://angular.io) – Gab Jan 29 '18 at 14:29
  • @Gab can you put some link which will help me please ? – uday214125 Jan 29 '18 at 14:32
  • Honesly, a quick google search can lead you to the answer. You need to research your question before posting on stackoverflow – Gab Jan 29 '18 at 14:34
  • 1
    Possible duplicate of [Angular 2 - How to trigger a method on a child from the parent](https://stackoverflow.com/questions/37635404/angular-2-how-to-trigger-a-method-on-a-child-from-the-parent) – Gab Jan 29 '18 at 14:35

1 Answers1

0

Several ways, you can either bind it via a service, or expose the method as an output on the child component directly, or maybe get a reference of the child component on the parent and then call the method from child instance, or have an observable passed from parent to child, that the child listens to. Here's that first example:

class ChildComponent {
  constructor(private gateService: GateService) {}
  ngOnInit() {
    this.gateService.openGate$.subscribe((params: any) => this.onGateOpen(params));
  }
  private onGateOpen(params) {
  }
}


class ParentComponent {
  constructor(private gateService: GateService) {}
  openGate() {
    this.gateService.openGate('some param');
  }
}

class GateService {
  openGate$: Subject<string> = new Subject();
  openGate(param) {
    this.openGate$.next(param);
  }
}

Read more about it all here.

Zlatko
  • 18,936
  • 14
  • 70
  • 123