5

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

3 Answers 3

18

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'];
});
Sign up to request clarification or add additional context in comments.

1 Comment

This worked great for me. Question: How do I make this work with a direct url? I get an error: "Refused to apply style from 'localhost:4200/userAdmin/styles.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled."
1

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.