I've done this before but this time I can't get this to work.
My problem is that my component is not recieving any object (and that's weird enough because I can print variables).
This is my service:
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class InfoService {
listCameras: any[] = [];
constructor(private http: Http) { }
getCameras() {
const headers = new Headers();
headers.append('Content-Type', 'application/json');
return this.http.get('http://localhost:3000/info/detalleCamaras', { headers: headers })
.map(res => {
this.listCameras = res.json();
console.log(this.listCameras);
return this.listCameras;
});
}
}
The output in the console log is fine
Now, when I go to /detalleCamaras component:
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Http, HttpModule, Response } from '@angular/http';
import { Headers } from '@angular/http';
import { InfoService } from './../../services/info.service';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-cameras',
templateUrl: './detalle-cameras.component.html',
styles: []
})
export class DetalleCamerasComponent implements OnInit {
listCameras: any[] = [];
constructor(private _http: Http,
private _info: InfoService) { }
ngOnInit() {
this._info.getCameras().subscribe(data => this.listCameras = data);
console.log(this.listCameras);
}
}
The output is empty but I can get to print a list into a table because of my service. But what I want to do is consume the JSON from my service.
What I'm doing wrong?
Edit:
Component.html
<table class="table table-hover">
<tbody>
<tr *ngFor="let camera of listCameras; let i = index">
<td scope="row">
{{camera.camera}}
</td>
<td>
{{camera.date}}
</td>
<td>N of Cameras</td>
<td>
<i class="fa fa-check icon-status-ok" aria-hidden="true"></i>
</td>
</tr>
</tbody>
</table>