1

enter image description hereI am new to angular I was given HTTP get service url to get the data from server How can I subscribe it and get the data.

/dashboard/graphics-chart/v1/{roleId}.

below is the method i have written in service file

  getgraphdata(defaultroleID): Observable<any> {
    return this.apiService.get(WebServiceUrl.dashboard.getPiechartData + '/' + defaultroleID)
      .pipe(map(data => data));
  }

How can I subscibe it in TS file.

4
  • 1
    is this method compiling with no error? Commented Feb 24, 2020 at 10:59
  • There is an error. Commented Feb 24, 2020 at 11:05
  • if the above apiService is not a HttpClient use a HttpClient to do http calls. my advice take a look at angular httpclient: angular.io/guide/http Commented Feb 24, 2020 at 11:11
  • We have a separate file where all HTTP calls are written we are just importing the same file in the above case all the https calls are written in apiservice instance. Commented Feb 24, 2020 at 11:20

3 Answers 3

1

Define your service in component constructor and call your servis method as following:

this.yourServiceName.getgraphdata(this.defaultroleID).subscribe(res=>{
   console.log(res)
});
Sign up to request clarification or add additional context in comments.

1 Comment

Hi this is my service url /dashboard/graphics-chart/v1/{roleId}. There is a error in my above mentioned code can you please check in to it.
0

You can subscribe in the following way in your component and to read the roleId, you can inject the route.

constructor(
    private yourService: YourService,
    private route: ActivatedRoute,
) { }

yourFunction() {
    let roleId = this.route.snapshot.paramMap.get('roleId');
    this.yourService.getgraphdata(roleId )
      .subscribe(result => ...do something with result);
}

3 Comments

How about type roleID which was given in my service.
@rohitsiriga updated, you need to inject the ActivatedRoute and then read the value roleId
@Ram Did it help you?
0

Subscribe to it as you would to any other Observable. To obtain parameters from the current URL, subscribe to paramMap of the ActivatedRoute. The following component makes your HTTP request whenever the role ID changes.

Component:

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

export class AppComponent implements OnInit {
  constructor(
    private dashservice: DashService, 
    private _route: ActivatedRoute ) { }
}

ngOnInit() {
  // get role ID from activated route
  this.route.paramMap.subscribe(
    (params: ParamMap) => { this.getchartdata(params.get('roleId')); }
  );
}

// use roleId 'default' if no parameters are passed
private getchartdata(roleId = 'default') {
  this.dashservice.getgraphdata(roleId).subscribe(
    response => {
      // handle response
    },
    error => {
      // handle error
    }
  );
}

Also seeing that you don't transform your data in the HTTP call, you could remove .map and return the HTTP response directly.

getgraphdata(defaultroleID): Observable<any> {
  return this.apiService.get(WebServiceUrl.dashboard.getPiechartData + '/' + defaultroleID);
}

5 Comments

Hi this is my service url /dashboard/graphics-chart/v1/{roleId}. There is a error in my above mentioned code can you please check in to it
Does WebServiceUrl.dashboard.getPiechartData return the correct URL? Does it point to your URL `/dashboard/graphics-chart/v1'?
Then what error are you receiving? Can you please post the error?
You declare defaultroleID but aren't initializing it. I think you meant to pass it as an arugment. In that case, do getChartData(defaultroleID: any). If not, do let defaultroleID = <valid ID>;
If you'd like to get the roleId from the URL, use ActivatedRoute. Please see my updated answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.