I'm trying to get startdate from the URL.
The URL looks like http://sitename/booking?startdate=28-08-2017
My code is below:
aap.module.ts
import {...};
@NgModule({
declarations: [
AppComponent, ModalComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
JsonpModule,
ReactiveFormsModule,
RouterModule.forRoot([{
path: '',
component: AppComponent
},
]),
],
providers: [ContactService, AddonService, MainService],
bootstrap: [AppComponent]
})
export class AppModule { }
aap.component.ts
import {...}
import {Router, ActivatedRoute, Params} from '@angular/router';
constructor(private activatedRoute: ActivatedRoute) {
// subscribe to router event
this.activatedRoute.queryParams.subscribe((params: Params) => {
console.log(params);
});
}
But its giving the below error
Unhandeled Promise rejection: No base href set. Please provide a value for the APP_BASE_HREF token or add a base element to the document. ; Zone: ; Task: Promise.then ; Value: Error: No base href set. Please provide a value for the APP_BASE_HREF token or add a base element to the document.
How does Angular know the base href?
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');
}
Update
I belive Dimitry Grinko's answer in this post is better than this one.
Old answer
This should do the trick retrieving the params from the url:
constructor(private activatedRoute: ActivatedRoute) {
this.activatedRoute.queryParams.subscribe(params => {
let date = params['startdate'];
console.log(date); // Print the parameter to the console.
});
}
The local variable date should now contain the startdate parameter from the URL. The modules Router and Params can be removed (if not used somewhere else in the class).
constructor(private activatedRoute: ActivatedRoute) {
}
ngOnInit() {
this.activatedRoute.params.subscribe(paramsId => {
this.id = paramsId.id;
console.log(this.id);
});
}
In angular, They separate it into 2 kind of url.
URL pattern /heroes/:limit
. Example: /heroes/20
route.snapshot.paramMap.get
.route.paramMap
to get paramsURL pattern /heroes
. Example: /heroes?limit=20
route.snapshot.queryParamMap
Reference: All you need to know about Angular parameters
The accepted answer uses the observable to retrieve the parameter which can be useful in the parameter will change throughtout the component lifecycle.
If the parameter will not change, one can consider using the params object on the snapshot of the router url.
snapshot.params
returns all the parameters in the URL in an object.
constructor(private route: ActivateRoute){}
ngOnInit() {
const allParams = this.route.snapshot.params // allParams is an object
const param1 = allParams.param1 // retrieve the parameter "param1"
}
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