get params angular code example

Example 1: get url params angular

import { ActivatedRoute, Params } from '@angular/router';

@Component(...)
export class MyComponent implements OnInit {
   constructor (private activatedRoute: ActivatedRoute){}
   
   ngOnInit() {
    this.activatedRoute.params.subscribe((params: Params) => {
      if (params.myParam){
        // Do something
      }
    });
   }
}

Example 2: angular get url params

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 3: angular routing url params

const appRoutes: Routes = [
  { path: 'crisis-center/:param1', component: CrisisListComponent },
  { path: 'hero/:param2',      component: HeroDetailComponent },
];

@NgModule({
  imports: [
    RouterModule.forRoot(
      appRoutes,
      { enableTracing: true } // <-- debugging purposes only
    )
    // other imports here
  ],
  ...
})
export class AppModule { }

Example 4: router params angular

// Navigate and send Params
this.router.navigate(['/users/edit/', user.id]);

Tags:

Misc Example