angular通过typescript文件中的数组循环

时间:2017-10-26 16:15:05

标签: angular

我有一个返回Json

的component.service
export interface ILocations {

    LocationID: string;
    LocationName: string;

}

服务

getUserLocations(UserID: string):Observable<ILocations[]> {
    return this._http.get(environment.BASE_API_URL + 'GetUserLocations/' + UserID)
        .map((response: Response) => <ILocations[]>response.json())
        .do(data => console.log('All' + JSON.stringify(data)))
        .catch(this.handleError);
}

Json从控制台打印出来:

All {
  "result": [
    {
      "LocationID": "01",
      "LocationName": "01"                             "
    },
    {
      "LocationID": "02",
      "LocationName": "01"
    }
  ]
}

这是我的component.ts文件,我只是试图遍历数组,因为我需要在字符串中提取用逗号分隔的LocationID。例如(看到上面的json)我需要一个像这样的字符串

location: string;
location= '01,02' 

我认为我的数组没有加载,我试图仔细检查值,但我得到错误。不确定我缺少什么,这就是我订阅服务的方式

  locations: ILocations[] = null;

    ngOnInit(): void {

        console.log("id : " + this.userID)
        this.GetUserLocations(this.userID)

        for (let location of this.locations) {
            console.log('code ' + location.LocationID.toString());
       }//this is causing error

    }

    GetUserLocations(PNSLUID: string) {

        this._dashboardService.getUserLocations(PNSLUID)
            .subscribe(
            data => {
                this.locations = data;
                console.log('location ' + this.locations[0].LocationID);//this is causing error 
            },
            error => console.log('GetControls Method: ' + <any>error, 'alert alert-danger'));
    }

********** UPDATE *************** ************************************************** * 改变了我的服务

getUserLocations(UserID: string){


    return this._http.get(environment.BASE_API_URL + 'GetUserLocations/' + UserID)
        .map((response: Response) => response.json())
        .do(data => console.log('All' + JSON.stringify(data)))
        .catch(this.handleError);

}

component.ts(只返回第一个raw?)

  locations: ILocations[] = null;

    ngOnInit(): void {


        this.GetUserLocations(this.userID)


    }

    GetUserLocations(PNSLUID: string) {

        this._dashboardService.getUserLocations(PNSLUID)
            .subscribe(
            data => {
                this.locations = data.result;

                for (let location of this.locations) {
                    console.log('code ' + location.LocationID.toString());
                }//this is returning only the first raw.
            },

            error => console.log('GetControls Method: ' + <any>error, 'alert alert-danger'));




}

1 个答案:

答案 0 :(得分:2)

我认为你有两个错误

  1. 在您的服务方法getUserLocations
  2. 修改map方法只返回

    下面的json liek
      getUserLocations(UserID: string):Observable<ILocations[]> {
            return this._http.get(environment.BASE_API_URL + 'GetUserLocations/' + UserID)
                .map((response: Response) => response.json())
                .do(data => console.log('All' + JSON.stringify(data)))
                .catch(this.handleError);
        }
    
    1. 然后在GetUserLocations中,因为您在“result”中获取了相关数据,请参阅之前发布的console.log输出。

      GetUserLocations(PNSLUID:string){

          this._dashboardService.getUserLocations(PNSLUID)
              .subscribe(
              data => {
                  this.locations = data.result;  //**Notice result here**
                  console.log('location ' + this.locations[0].LocationID);//this is causing error 
              },
              error => console.log('GetControls Method: ' + <any>error, 'alert alert-danger'));
      }
      

      这有望解决问题:)