To set imperatively a route param in a router-outler
we do:
this.router.navigate(['/hero', hero.id]);
How do I imperatively set a route parameter in named outlets ?
this.router.navigate([{ outlets:{modal: 'modal/user'} }]);
Right now I concatenate string to set a route param:
this.router.navigate([{ outlets: {modal: 'modal/user' + '/' + 'this.id'} }]);
make sure your routing file have :id in modal component like this in mine
{ path: 'component-aux/:id', component: ComponentAux, outlet: 'sidebar' }
and your calling route should be like this
id: string="any value you want to set";
this.id= input || 'not set';
|| will set default value if no value is specified
<a [routerLink]="[{ outlets: { 'sidebar': ['component-aux', this.id] } }]">Component Aux</a>
and your aux component should be like this
import {Component} from '@angular/core';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'component-aux',
template: 'Component Aux'
})
export default class ComponentAux {
private id;
private sub: any;
constructor(private route: ActivatedRoute) {}
private ngOnInit() {
this.sub = this.route.params.subscribe(params => {
this.id = +params['id']; // (+) converts string 'id' to a number
console.log(this.id);
});
}
private ngOnDestroy() {
this.sub.unsubscribe();
}
}
here is the live plnkr link https://plnkr.co/edit/5aP6MgTRzXr5Urq5fr2J?p=preview . i hope this will help :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With