I want to return the data from service to component but when i user the service variable to return the data no error appear but just undefined.... kindly guide me through what is just wrong. When using ionic same logic i applied in angular i get the data but not here in angular 4 application.
signal.service.ts
when i do console.log(this.signal)
give me an object array correctly.
import { Injectable } from '@angular/core';
import {HttpModule, Http} from '@angular/http';
@Injectable()
export class SignalsService {
signal : Array<any>;
constructor(public _http:Http) {
this._http.get('http://localhost/4x/data.php')
.subscribe(res =>{
this.signal = res.json().data;
return this.signal
});
}
}
My component here when i do console.log the signal returning data from service it says Undefined
home.component.ts
import { Component, OnInit } from '@angular/core';
import {HttpModule, Http} from '@angular/http';
import {SignalsService} from '../signals.service';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css'],
})
export class HomeComponent implements OnInit {
sig :Array<any>;
constructor(public _http:Http,public _signa:SignalsService) {
}
ngOnInit() {
console.log(this._signa.signal);
}
Moreover
when i directly insert the service request of HTTP in home.component.ts in ngOnInit section it works and returning the data but why my service is not working.