0
I created a service to load a Json API:
import { Injectable } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/do';
@Injectable()
export class PlanosService {
//get Json Servicos
planos: Object[] = [];
constructor(http: Http) {
http
.get('https://api.carguruclub.com/v1/servicos')
.map(res => res.json())
.subscribe(planos => {
this.planos = planos;
console.log(this.planos);
}, erro => console.log(erro));
}
}
In callback it returns me the object, but my doubt is on how to manipulate so that I can display your fields in my view. Example:
{
"products": [
{
"_id": "5886f570f0d1fb003caea75b",
"id": "lavagem_padrao",
"name": "Lavagem Padrão",
"desc": "Lavagem completa",
"recommendation": "Carguru recomenda a lavagem Padrão se você procura a melhor lavagem básica do mundo. Inclui calibragem dos pneus e o nosso famoso checklist."
}
]
}
I wanted to display the name field of the Products object: {products.name}} But I’m stuck on how to do this part. Thank you in advance
console.log(this.planos);
returns the json that of the question? why are you using.subscribe()
instead of.then()
?– mercador
@merchant yes, he returns the objects
– Julio Rodrigues
I don’t know if I understand this correctly, but wouldn’t it just be taking the data and interpolating it into the view? On its component : ` record: any; ngOnInit() { const id = this.route.snapshot.paramMap.get('id'); this.itemSvc.getReg(id) . subscribe(data) => { this.record = data console.log('Category Found ' + JSON.stringify(data); }); } And then in the view {{ record.campo_que_vc_quer_display }} Sorry if I didn’t get it.
– duardbr
try to change
planos: Object[] = [];
forplanos: any;
If it works/doesn’t work, please let me know– Gaspar