constructor(private activatedRoute: ActivatedRoute) {
this.activatedRoute.queryParams.subscribe(params => {
let date = params['startdate'];
console.log(date); // Print the parameter to the console.
});
}
Routes
export const MyRoutes: Routes = [
{ path: '/items/:id', component: MyComponent }
]
Component
import { ActivatedRoute } from '@angular/router';
public id: string;
constructor(private route: ActivatedRoute) {}
ngOnInit() {
this.id = this.route.snapshot.paramMap.get('id');
}
import { ActivatedRoute } from "@angular/router";
//import ActivatedRoute in constructor()
private $route: ActivatedRoute
// in ngOnInit() call
//myvar is the variable where you want to store your param
this.$route.params.forEach(param =>
this.myvar = param['whatever your param name is']
);
// example url: details?id=2
constructor(private activatedRoute: ActivatedRoute) {
this.activatedRoute.queryParams.subscribe(params => {
console.log(params); // Prints {id: "2"}
});
}
// ...
import { ActivatedRoute } from '@angular/router';
@Component({ ... })
export class BookComponent implements OnInit {
orderby: string;
constructor(private route: ActivatedRoute) { }
ngOnInit() {
this.route.queryParams
.subscribe(params => {
console.log(params); // { orderby: "price" }
this.orderby = params.orderby;
console.log(this.orderby); // price
}
);
}
}