1

I have testTry() function in Component1 which accepts one parameter and prints the value.

export class Component1 implements OnInit {

 testTry(name:any){
 console.log("Name-->",name);}

 ngOnInit(){    }

}

I have component2 with a function sampleCall() in which i need to call a function of component1 by sending a parameter

export class Component2 implements OnInit {

 sampleCall(){
  let a = "Sravya";
  testTry(a);
}

 ngOnInit(){    }
}

How would I call a function from component1 to component2 without involving HTML?

halfer
  • 19,824
  • 17
  • 99
  • 186
Sravya
  • 65
  • 13

6 Answers6

0

You can use @ViewChild,

We can use this to get control for Child Component inside Parent Component. When we used it, we will get the child Component as Object so that we are able to call the methods and variables of the the Child Component.

Component2.ts

 @ViewChild(Component1) component1: Component1;

 sampleCall() {
  let a = "Sravya";
  this.component1.testTry(a);
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Ganesh
  • 5,808
  • 2
  • 21
  • 41
0

You can do:

  1. Create a service and move the method inside the service

  2. Inject component1 into component2 (not recommended)

Try:

export class Component2 implements OnInit {

constructor(private comp1: Component1){}

sampleCall(){
  let a = "Sravya";
  this.comp1.testTry(a);
}

 ngOnInit(){    }
}
Shashank Vivek
  • 16,888
  • 8
  • 62
  • 104
0

Apart from the above solutions, which are logically correct, you can also use Inheritance (a less used feature of typescript).

Keep all the functions in a separate file like common-logic.ts and other components can simply access them using extends keyword.

export class Component2 extends CommonLogic {}

Component2

export class Component2 implements OnInit extends CommonLogic {

 sampleCall(){
  let a = "Sravya";
  testTry(a);
 }

CommonLogic

export class CommonLogic {

 testTry(name:any){
   console.log("Name-->",name);
 }

}

Note: Angular only allows inheritance up to one level.

Raj
  • 1,100
  • 3
  • 20
  • 33
0

You can use EventEmmiter as below.

ChangeService.ts

import {EventEmitter} from 'angular2/core';
export class ChangeService {
  calltestTry: EventEmitter<string> = new EventEmitter();
  constructor() {}
  emitcalltestTryEvent(data) {
    this.calltestTry.emit(data);
  }
  getcalltestTryEmitter() {
    return this.calltestTry;
  }
}

component1.ts

@Component({
  selector: 'app-component1',
})
export class ComponentOne {
  item: number = 0;
  subscription: any;
  constructor(private changeService:ChangeService) {}
  ngOnInit() {
    this.subscription = this.changeService.getcalltestTryEmitter()
      .subscribe(item => this.testTry(item));
  }
  testTry(item: string) {
     console.log(item);
  }
  ngOnDestroy() {
    this.subscription.unsubscribe();
  }
}

component2.ts

@Component({
  selector: 'app-component2',
})
export class ComponentTwo {
  item: number = 0;
  subscription: any;
  constructor(private changeService:ChangeService) {}
  ngOnInit() {
  }

  sampleCall(item: number) {
     let a = "Sravya";
     this.changeService.emitcalltestTryEvent(a);
  }
  ngOnDestroy() {
    this.subscription.unsubscribe();
  }
}
Hardik Patel
  • 3,868
  • 1
  • 23
  • 37
0

There are actually 4 ways of doing this.

  1. Including the function in a service, so that you can inject it anywhere.
  2. Making your function static, so that you can access it using the Class. (Pros and Cons are there)
  3. Inject the component to the other component so that you can access it (Not recommended).
  4. Pass your function as an input parameter to the other component.
Dulanjaya Tennekoon
  • 2,408
  • 1
  • 18
  • 31
0

You can use ComponentFactoryResolver to get components and use their properties.

export class Component1 implements OnInit {

 testTry(name:any){
   console.log("Name-->",name);
 }

 ngOnInit(){    }

}

import {ViewChild, ComponentFactoryResolver, ViewContainerRef } from '@angular/core';
export class Component2 implements OnInit {

  @ViewChild('parent', { read: ViewContainerRef }) container: ViewContainerRef;

  constructor(
    private _cfr: ComponentFactoryResolver
  ) { }

  sampleCall(){
    const comp = this._cfr.resolveComponentFactory(Component1);
    const component1Ref = this.container.createComponent(comp);
    const component1 = component1Ref.instance;
    component1._ref = component1Ref;

    let a = "Sravya";
    component1.testTry(a);
 }

  ngOnInit(){    }
}
Nitin Jangid
  • 164
  • 1
  • 3