Angular2嵌套http get请求
我有两个api/resource/Item返回Angular2嵌套http get请求,angular,Angular,我有两个api/resource/Item返回 {data: [ {name: "BE106359"}, {name: "NDBKR5050"} ]} { data: { net_weight: 0, max_discount: 0, item_name: "NEC | BE106359 | CD-PRTA - ISDN PRI TRUNK BLADE, SV8xxx", website_warehouse: "Stores - CT", default_material_request_ty
{data: [
{name: "BE106359"},
{name: "NDBKR5050"}
]}
{
data: {
net_weight: 0,
max_discount: 0,
item_name: "NEC | BE106359 | CD-PRTA - ISDN PRI TRUNK BLADE, SV8xxx",
website_warehouse: "Stores - CT",
default_material_request_type: "Purchase",
disabled: 0,
name: "BE106359"
......
}}
对于返回的项目详细信息api/resource/Item/{name}
{data: [
{name: "BE106359"},
{name: "NDBKR5050"}
]}
{
data: {
net_weight: 0,
max_discount: 0,
item_name: "NEC | BE106359 | CD-PRTA - ISDN PRI TRUNK BLADE, SV8xxx",
website_warehouse: "Stores - CT",
default_material_request_type: "Purchase",
disabled: 0,
name: "BE106359"
......
}}
我的angular2代码可以遍历详细信息,但打印未定义的值,因此无法在HTML模板上获取任何值
@Injectable()
export class ProductsService {
getProductDetails() {
return this.http.get("https://domain/api/resource/Item")
.map(
p => {
let products = p.json().data;
products.forEach(m =>
this.http.get('https://domain/api/resource/Item/'+ m.name)
.map(details=> details.json())
.subscribe(details => details.data) // <-- get values when trying to console print out
);
});
}}
//calling service from component
export class ProductsComponent implements OnInit {
products;
constructor(private service: ProductsService) {
this.service.getProductDetails().subscribe(products => this.products = products); // <---- console.log(products) print undefined value
}}
@Injectable()
导出类产品服务{
getProductDetails(){
返回此.http.get(“https://domain/api/resource/Item")
.地图(
p=>{
让products=p.json().data;
products.forEach(m=>
this.http.get('https://domain/api/resource/Item/“+m.name)
.map(details=>details.json())
.subscribe(details=>details.data)//this.products=products);//这解决了我的问题,我所需要的一切mergeMap
getProductDetails() {
let Products: any[] = [];
return this.http.get('https://domain/api/resource/Item')
.map(res => res.json().data)
.mergeMap(list => list)
.mergeMap(name => {
return this.http.get("https://domain/api/resource/Item/" + name['name'])
})
.map(res => {
Products.push(res.json().data)
{ return Products; }
})
}
//then call it from my component
this.service.getProductDetails().subscribe(res=>this.products= res );
好吧,在注释掉console.log的地方没有局部变量products
,所以它甚至不应该编译。您正在执行的实际代码是什么?请帮自己一个忙,并指定方法的返回类型。您会发现您的代码没有多大意义。对外部映射的回调,p=>…
,确实如此实际上没有返回任何东西。this.products=products
而不是this.products==products
可能的重复