get url variable ngular 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 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 { }

Tags:

Html Example