r/angular • • 4d ago

Angular API implementation

I’m a student that is working with angular for the first time, I have an assignment that requires implementing an api to our project. My teacher has no information on how to unwrap the response using parse for httpResource. There also is no information about mapping with httpclient & observables.

The documentation that is recommended seems completely irrelevant to my the api I have because my api is returning as an array.

How can I get value from the documentation or where should I look to figure this out. I feel like I’ve been having this problem with everything because the documentation never seems relevant. Even with certain concepts, there is things that I know but I am struggling with knowing what or how I can write things to do the task I need completed.

Any assistance would be appreciated.

2 Upvotes

23 comments sorted by

View all comments

-11

u/AdministrativeHost15 4d ago
getData(
): Observable<any[]> {
  let url = 
location
.origin + '/api/data?';
  const url1 = new URL(url);

  if (
location
.hostname === 'localhost') {
    url1.port = '3000';
  }
  url = url1.toString();
  return this.httpClient.get<any[]>(url);
}

Note: Change 'any' to the type that the API returns.

6

u/Xsiah 4d ago

Don't just give people homework answers without explaining what the code does

6

u/couldhaveebeen 4d ago

Funny thing is, it doesn't answer the question anyway

4

u/Responsible-Cold-627 4d ago

It's also dumb as hell manually adding the origin to the URL. Looks like he doesn't know how relative URLs work.