Tôi cố gắng để in kết quả của http
cuộc gọi trong Angular
sử dụng rxjs
Làm thế nào để lấy dữ liệu từ quan sát được trong angular2
Xét đoạn mã sau
import { Component, Injectable, OnInit } from '@angular/core';
import { Http, HTTP_PROVIDERS } from '@angular/http';
import 'rxjs/Rx';
@Injectable()
class myHTTPService {
constructor(private http: Http) {}
configEndPoint: string = '/my_url/get_config';
getConfig() {
return this.http
.get(this.configEndPoint)
.map(res => res.json());
}
}
@Component({
selector: 'my-app',
templateUrl: './myTemplate',
providers: [HTTP_PROVIDERS, myHTTPService],
})
export class AppComponent implements OnInit {
constructor(private myService: myHTTPService) { }
ngOnInit() {
console.log(this.myService.getConfig());
}
}
Bất cứ khi nào tôi đã cố gắng để in ra kết quả của getconfig
nó luôn trả lại
Observable {_isScalar: false, source: Observable, operator: MapOperator}
mặc dù tôi trả lại đối tượng json thay thế.
Làm cách nào để in kết quả của getConfig
?
bạn đã nhận được các giải pháp? – ramya