40

First of all, I am not sure if this is the right way, to achieve what I want.. I am trying to add an export binding like the following to a router.navigate()-method:

<call [caller]="caller"></call>

The problem is that I never use the directive, but instead adress it through a router:

acceptCall() {
   this.router.navigate(["call"]);
}

How can I achieve the same export binding from the first example in the acceptCall()-method? I Have already added an Input() variable and tried it with queryParams like this:

@Input() caller: Caller;
acceptCall(caller) {
    this.router.navigate(["call"], {queryParams: caller});
}

But this does not work.

6
  • Almost ! try this.router.navigate(["call", caller]); Commented Jun 29, 2017 at 9:39
  • Doesnt work for me, Do I have to modify my RouterModule.forRoot? mine currently looks like: { path: 'call', component: CallComponent } Commented Jun 29, 2017 at 9:51
  • Well yes, {path: 'call/:caller'} and you can get it with this.route.params.subscribe((params: Params) => this.myParam = params['caller']); in your subcomponent (route is an ActivatedRoute) Commented Jun 29, 2017 at 9:57
  • I have updated my module, but dont quite get the other stuff :D Where do you declare Params , myParam? could you maybe provide a full code example of the class as an answer? Commented Jun 29, 2017 at 10:06
  • @reveN: it's not very obvious how your code is organized. It would be easier to help you if you said clearly where all the bits of code you provided are located. Commented Jun 29, 2017 at 10:19

1 Answer 1

60

Following my comments you have to :

1 - Redefine your route

{path: 'call/:caller', component: MyComponent }

2 - Change your navigation in the parent component

this.router.navigate(["call", caller]);

3 - In the child component, get the param

import { ActivatedRoute } from '@angular/router';
// code until ...
myParam: string;
constructor(private route: ActivatedRoute) {}
// code until ...
ngOnInit() {
    this.route.params.subscribe((params: Params) => this.myParam = params['caller']);
}
Sign up to request clarification or add additional context in comments.

9 Comments

mhh Ive done everything exactly as you said, but my router roots now back to my "home"-component instead of the call-component, when I call the method
I used calls like this.router.navigate(['../user', id], { relativeTo: this.route }); to be sure that it would redirect to the right url. Could you try it ?
Oh ! Then use either a service or a Parent-Child relation. route params aren't made for that.
just for completeness, you also need to import Params from @angular/router
Coment with this.router.navigate(['../user', id], { relativeTo: this.route }); helped me (I used ActivatedRoute for router ). It will be good to be included in answer.
|

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.