2
Guys, I need help with Angular 4... I need to consume an api (json-server)... for testing, I put the content of Json directly in the code, as below... but now I need to replace it with the address of the api (localhost:port/communiqué).
Can someone tell me how to do this?
It’s all set up. I just need to know how to enter the server address. I’ve been searching since 5am but no results.
I appreciate anyone who can give me some guidance.
import { HttpClient } from "@angular/common/http"
import { Injectable } from '@angular/core'
import { Comunicados } from './shared/comunicados.model'
@Injectable()
export class ComunicadosService {
constructor(private http: HttpClient){}
public comunicados: Comunicados[] = [
{
"id": 1,
"title": "Titulo",
"seen": false,
"type": 0,
"description": "descricao.",
"fullDescription": "AAA",
"date": "2013-05-03 8:30"
},
}
public getComunicados(): Array<Comunicados> {
return this.comunicados
}
}
where’s your call code
ajax
?– Ricardo Pontual
Thank you for the reply, @Ricardopontual This is the contents of the module file: https://pastebin.com/zx87Jegv
– Bruno Sousa
Bruno, if you also have the javascript part that makes the call to the service helps us understand what is not working
– Ricardo Pontual
I’m actually using Typescript, Ricardo. The above code already works with the data of the json entered. But I need to remove this json that I pasted and use a URL from a Fake Api, you know?
– Bruno Sousa
So, "but now I need to replace this with the api address" to do this you need a call
ajax
, you’ve even injected the dependency ofHttpClient
but is missing use to call the api, this is missing in your code– Ricardo Pontual
You can do so: http.get(URL) . map(res => res.json()) . subscribe(data => console.log(date);
– Lucas Brogni