比较来自世界各地的卖家的域名和 IT 服务价格

Angular2 HTTP 使用 observables subscribe 显示数据 undefined

我不知道我做错了什么,但由于某种原因我无法读取数据,尽管数据来自服务器响应,甚至数据在方法内显示 service extractData, 当我放置控制台时,但在功能内的组件中 subscribe 他给了我一个无限期的价值。 帮助我做错了什么,我认为这是一个异步问题,但我不知道如何解决它。
任何帮助都将是有形的。
提前致谢

Component.ts


import { Component, Input, OnInit } from '@angular/core';
import {AdminService} from './admin.service';
import {logistics} from '../shared/model/logistics';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
import 'rxjs/add/operator/switchMap';
import 'rxjs/add/operator/toPromise';
import { Observable } from 'rxjs/Observable';
import {Response } from '@angular/http';
@Component/{
moduleId:module.id,
selector: 'admin',
templateUrl: 'admin.component.html',
styleUrls:['admin.component.css'],
providers:[AdminService]
}/

export class AdminComponent implements OnInit{
@Input// public allocatedAssetsList: logistics[];


mode = 'Observable';
public errorMsg = '';
constructor/private adminService: AdminService/ {

}

ngOnInit//{
this.listByEmpId/"123"/;

}

listByEmpId/empId:string/{

this.adminService.getAllocatedAssets/empId/.subscribe/
res => this.allocatedAssetsList = res,
error => this.errorMessage = <any>error/;
}
}


Service.ts


import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Hero } from './hero';
import { Observable } from 'rxjs/Observable';
import { Headers, RequestOptions } from '@angular/http';
import {logistics} from '../shared/model/logistics';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
import 'rxjs/add/operator/switchMap';
import 'rxjs/add/operator/toPromise';


@Injectable//
export class AdminService {
constructor /private http: Http/ {}
private listAssetsURL = '/api/logistics/list/'; // URL to web API

private extractData/res: Response/ {
let body = res.json//;
return body || { };
}

private handleError /error: any/ {
// In a real world app, we might use a remote logging infrastructure
// We'd also dig deeper into the error to get a better message
let errMsg = /error.message/ ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error/errMsg/; // log to console instead
return Observable.throw/errMsg/;
}

getAllocatedAssets /empId: string/: Observable<logistics[]> {

this.listAssetsURL+= empId;
//let body = JSON.stringify/{ empId }/;
let headers = new Headers/{ 'Content-Type': 'application/json' }/;
let options = new RequestOptions/{ headers: headers }/;

return this.http.get/this.listAssetsURL/
.map/this.extractData/
.catch/this.handleError/;
}
}


</logistics[]></any>
已邀请:

窦买办

赞同来自:

listByEmpId/empId:string/{

this.adminService.getAllocatedAssets/empId/.subscribe/
res => {
this.allocatedAssetsList = res;
console.log/this.allocatedAssetsList/;
},
error => this.errorMessage = <any>error/;
}


</any>

董宝中

赞同来自:

可能是因为你想要访问你的东西 allocatedAssetsLists 在实际从服务中返回数据之前。

如果您在模板中访问它,您可以使用简单
ngIf

, 要确保在数据可用后才能尝试显示它。

如果不是,我们需要有关您的问题的其他信息来帮助。

要回复问题请先登录注册