1

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

1

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

5 Comments

hi thanks for reply i tried that example with shared servive , it threw error something about querry, can you show me example here? or just rough code??
There are probably dozens of answers to this question and the linked tutorial shows in detail how to do it. If you get an error about "query" then you are missing an import or didn't not provide a service correctly.
this is how i created service @Injectable() export class AuthLoginService { constructor(private Service: AppComponent) { } Error() { this.Service.authcheck();} }
I think the new RC.2 version provides a better error message for this problem. It's usually caused by a constructor parameter type not being properly imported. It could also be that you have a cyclic dependency which DI doesn't allow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.