Angular 为什么此服务调用返回数组中对象的正确计数,但所有索引都是相同的对象?

Angular 为什么此服务调用返回数组中对象的正确计数,但所有索引都是相同的对象?,angular,Angular,使用Angular2服务从API端点获取信息。与Postman一起测试API调用时,数据是正确的,如下所示: 但当应用程序调用api时,数组的内容都是相同的: 我使用angular中的Http模块调用API,如下所示: public getPositionsById(id: number){ let fullUrl = `records/positions/${id}`; return this.http.get(fullUrl) .map((i)

使用Angular2服务从API端点获取信息。与Postman一起测试API调用时,数据是正确的,如下所示:

但当应用程序调用api时,数组的内容都是相同的:

我使用angular中的Http模块调用API,如下所示:

  public getPositionsById(id: number){
    let fullUrl = `records/positions/${id}`;
    return this.http.get(fullUrl)
            .map((i) => this.mapPosition(i))
            .catch((err) => {
              console.error(err);
              return err;
            });
  }
  private mapPosition(res: Response){
    let body = res.json();
    console.dir(body);
    let pos: PositionInfo[] = new Array<PositionInfo>();
    let p: PositionInfo = new PositionInfo();
    for(let b of body){
      p.IndivId = b.IndivId;
      p.positionId = b.positionId;
      p.positionCode = b.positionCode;
      p.positionTitle = b.positionTitle;
      p.districtServed = b.districtServed;
      p.areaServed = b.areaServed;
      p.reason = b.reason;
      pos.push(p);
    }
    console.dir(pos);
    return pos;
  }
public getPositionsById(id:number){
设fullUrl=`records/positions/${id}`;
返回此.http.get(fullUrl)
.map((i)=>此.map位置(i))
.catch((错误)=>{
控制台错误(err);
返回错误;
});
}
私有映射位置(res:Response){
让body=res.json();
console.dir(body);
let pos:PositionInfo[]=新数组();
设p:PositionInfo=newpositioninfo();
为了(让b离开身体){
p、 individd=b.individd;
p、 位置ID=b.位置ID;
p、 位置代码=b.位置代码;
p、 职位头衔=b.职位头衔;
p、 districtServed=b.districtServed;
p、 areaServed=b.areaServed;
p、 原因=b.原因;
正推(p);
}
控制台目录(pos);
返回pos;
}

实例化for循环中的
p
对象

let p: PositionInfo ;
    for(let b of body){
      p = new PositionInfo();/////  new refernce will be created every time
      p.IndivId = b.IndivId;
      p.positionId = b.positionId;
      p.positionCode = b.positionCode;
      p.positionTitle = b.positionTitle;
      p.districtServed = b.districtServed;
      p.areaServed = b.areaServed;
      p.reason = b.reason;
      pos.push(p);
    }

引用未更新

实例化for循环中的
p
对象

let p: PositionInfo ;
    for(let b of body){
      p = new PositionInfo();/////  new refernce will be created every time
      p.IndivId = b.IndivId;
      p.positionId = b.positionId;
      p.positionCode = b.positionCode;
      p.positionTitle = b.positionTitle;
      p.districtServed = b.districtServed;
      p.areaServed = b.areaServed;
      p.reason = b.reason;
      pos.push(p);
    }

引用未更新

由于后端的原因,我遇到了相同的问题,但我的问题是从缓存返回的。我如何防止它返回缓存结果?您必须在后端检查它是否确实从缓存返回,以及它们如何处理您的数据。再次查看代码,我没有在循环中重置position对象我也有同样的问题,因为我的后端,但我的是从缓存返回的。我如何防止它返回缓存结果?你必须在后端检查它是否真的从缓存返回,以及它们如何处理你的数据。再次查看代码,我没有重置回路中的position对象。我发现了这个问题,但从未发布过。感谢@Aravind发现此故障,但从未发布。谢谢@aravind