How to Get Query Parameters from URL in Angular

Now in the the component access the url parameters by subscribing to ActivatedRoute.queryParams observable.

url: http://localhost:4200/customer?name=test 
import { ActivatedRoute } from '@angular/router';

@Component({ ... })
export class BookComponent implements OnInit {
  
  name: string;
  
  constructor(private route: ActivatedRoute) { }

  ngOnInit() {
    this.route.queryParams
      .subscribe(params => {
        console.log(params); // { name: "test" }
        this.name= params.name;
        console.log(this.name); // test
      }
    );
  }
}
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments