I am new on angular2, I need some help. I am using below service class.
import {Injectable} from 'angular2/core';
import {Http} from 'angular2/http';
import 'rxjs/add/operator/map';
@Injectable()
export class UsersService {
private _url = "http://jsonplaceholder.typicode.com/users";
constructor(private _http: Http){
}
getUsers(){
return this._http.get(this._url)
.map(res => res.json());
}
}
when I am calling above service in below component, I get undefined value.
import{Component, OnInit} from 'angular2/core';
import{UsersService} from './users.service';
@Component({
selector: 'users',
template:'In users',
//templateUrl: 'app/users.component.html',
providers: [UsersService]
})
export class UsersComponent implements OnInit{
users: any[];
constructor(private _service: UsersService){
}
ngOnInit(){
this._service.getUsers()
.subscribe(result => this.users = result);
console.log(this.users);
}
}
But if I tried to log the value in the console in service class, its shows there. any help would be highly appreciable. Thanks