Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6 passing in parameters into URL

So I have an angular app that currently has a route such "urlname/stockdata". I would like to be able to pass in a stock symbol into that url such as "urlname/stockdata/AAPL". My app is making a call to a third party API with that symbol. Do you guys have any advice on how I can do that. Any help is appreciated. Thanks

like image 280
Vlad Bogza Avatar asked Dec 06 '22 10:12

Vlad Bogza


2 Answers

You can declare a route with parameters like this:

export const routes: Routes = [
  { path: 'urlname/stockdata/:id', component: MyComp}
];

Create a link in the template:

<a [routerLink]="['/urlname/stockdata', id]">MyLink</a>

Or navigate from the .ts:

 this.router.navigate(['/urlname/stockdata', id]);

Then you can read it with:

constructor(private route: ActivatedRoute) {}

this.route.params.subscribe(params => {
  this.id = params['id'];
});
like image 71
Maarti Avatar answered Jan 31 '23 08:01

Maarti


TS File

const routes: Routes = [{
  path: '',
  children: [{
    path: 'plans/:id',
    component: PlanListComponent,
    canActivateChild: [AuthGuard]
  }]
}];

HTML

<a class="dropdown-item" (click)="viewDetails(product.id)">View Detail</a>

TS

 viewDetails (productId) {
        this.router.navigate([ BASE_URL +"/plans/" + productId]);
    }
like image 32
Sandeep Patel Avatar answered Jan 31 '23 09:01

Sandeep Patel