简体   繁体   中英

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

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'];
});

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]);
    }

First of all , consider you have to send 4 parameters in URL for eg. Zone , State , MP & JC .
For this go to file sample.component.ts


 functionName() {
 let param={
      zone:this.selectZone,
      state:this.selectState,
      jc:this.stateJC,
      mp:this.MPdata
    }

 this.availservice.AvailReport(param)
      .subscribe((json) => {
      this.ArrayResponse= json;
      } );
}

Now come to file sample.service.ts

 AvailReport(param) {  
let UrlTemp= `http://11.111.11.11:3838/getdata/AVAILABILITY_${param.zone}_${param.state}_${param.jc}_${param.mp}.JSON?callback=true`;
    return this.http.get(UrlTemp);
  }

For sure your will get response like :

Request URL: http://10.141.55.49:3838/getdata/AVAILABILITY_ZoneName_StateName_MPName_JCName.JSON?callback=true

Using this method you pass any number of parameters in URL & Ask your backend team to recieve the parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM