Search code examples
angularangular2-components

calling parent component function from router component in angular2


I have two components as follow and I want to call a function from another component. component1 is parent i.e. appcomponent and component 2 is actually a child route component.

parent Component 1:

@component(
selector:'my-app'
)
export class com1{
function1(){...}
}

child route Component 2:

@component(
selector:'com2'
)
export class com2{
function2(){...
// i want to call function 1 from com1 here
}
}

I've tried using event emitter etc but its not working .can anyone help? this setup is basically for authentication call login box if not authenticated , if there is something i need to know about , will be helpful too


Solution

  • For communication with components added by the router use a shared service. Inputs and outputs are not going to work.

    For communication between components see https://angular.io/docs/ts/latest/cookbook/component-communication.html