2
abc.component.ts
import...
...
...
@component({ ... })

export class Abc {

  public foo(){
     console.log("execute foo in UserComponent's xyz() ");
  }
}

user.component.ts
import...
...
...
import {Abc} from '../utils/abc.component';
@Component({
    selector: 'user',
    templateUrl: 'app/user/user.html',
    providers: [HTTP_PROVIDERS],
    directives: [Abc]
})
export class UserComponent {

   xyz(){
      Abc.foo(); //--> ?
   }
}

I have imported Abc module in user.component.ts. How can I call Abc.foo() in UserComponent Class methods?

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0

You could use Parent calls a ViewChild scenario from official documentation Angula2 like this

Parent component

import {Component, ViewChild} from '@angular/core'

@Component({
  selector: 'my-app',
  template: `
    <div>
      <abc></abc>
    </div>`
})
export class App {
  @ViewChild(ABC)
  private abc:ABC;
  ngAfterViewInit() {
    this.abc.foo();
  }
}

Corresponding plunker example is here https://plnkr.co/edit/VPsMTtJ29XBWr7SCqbzA?p=preview

yurzui
  • 205,937
  • 32
  • 433
  • 399