简体   繁体   中英

How to pass id to get api url?(Angular)

I have to pass an id property from GET api to another GET api For example... I'm doing something like this..

https://jsonplaceholder.typicode.com/posts/${value} 

where ' ${value} ' has to be replaced by 1,2,... But this is showing me 404 not found error.. But if I try individually like this

https://jsonplaceholder.typicode.com/posts/1

I'm able to fetch results.. Please help me resolve this issue..

.ts file

viewPosts(value){
   console.log(value);
  this.apiService.getPosts(value).subscribe((result) => {
       console.log(result);
        ......

});  

 }

ApiService file:

getPosts(value:any):Observable<any>{

    const api='https://jsonplaceholder.typicode.com/posts/${value}';
    return this.http.get(api);
  }

you just have to change this code.

getPosts(value:any):Observable<any>{
  const api = `https://jsonplaceholder.typicode.com/posts/${value}`;
  return this.http.get(api);
}

you have to use template literal .

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