Angular 2从响应中提取数据

时间:2017-01-11 23:09:07

标签: angular

我试图从没有运气的回复中提取数据。你能告诉我问题在哪里吗?

this.tite = data.result[0].LinkID;
this.tite = data.result[1].LinkName;

component.html

<div class="team">
    <h3>{{title}}</h3>
<div>

对象

getLinks{"result":{"LinkID":1,"LinkName":"Financial Planning Reports Admin"}}

component.ts

import { Component, OnInit  } from '@angular/core';
import { NavMenuService } from './navmenu/navMenu.service';
//import { ITitle } from './app';

@Component({
    selector: 'pm-app',
    moduleId: module.id,
    templateUrl: 'app.component.html',
    providers: [NavMenuService]
})
export class AppComponent implements OnInit {
    pageTitle: any[];
    tite: string;
    titileID: number;
    errorMessage: string;
    constructor(private _navMenuService: NavMenuService) {

    }

    ngOnInit(): void {
        this._navMenuService.getLinkName(600860)
            .subscribe(
            data => {
                this.tite = data.result[0].LinkID;
                this.tite = data.result[1].LinkName;

            },
            error => this.errorMessage = <any>error);

    }

}

service.ts

getLinkName(LinkID: number) {

        return this._http.get(this._fileUploadAPI + 'GetLinks/'+ LinkID.toString())
            .map((response: Response) => response.json())
            .do(data => console.log('getLinks' + JSON.stringify(data)))
            .catch(this.handleError);

    }

1 个答案:

答案 0 :(得分:1)

从服务返回的数据的result属性不是基于上面的控制台输出的数组(而是它只是一个对象)。删除索引,它应该工作。

this.titleID = data.result.LinkID;
this.title = data.result.LinkName;