将json属性影响到angular2中的变量

时间:2016-05-30 13:25:08

标签: json angular

我试图通过Angular 2中的变量影响JSON属性的值,我试图在我的应用程序组件中访问它,如下所示:

export class AppComponent{

    count : any;

    constructor(private _getService : GetService) {

        this._getService.getPosts(this.urlG)
            .subscribe(
                result => this.actionsG = result, 
                error=> console.error('Error: ' + error), 
                ()=> console.log('finish! ActionsG')
            ); // getting the JSON FILE

        this.aCount = this.actionsG.actionList[0].count; 

        // that's what i'm trying to do to get the values of count in the json
        // file and stock it into aCount..
    }
}

我的JSON文件:

{
    "actionList": {
        "count": 70, //
        "list": [
            {
                "Person": {
                    "name": "David",
                    "age": "30",
                    "id": "D5UG8R",
                   ...

1 个答案:

答案 0 :(得分:1)

this.aCount = this.actionsG.actionList[0].count; 

之前执行
result => this.actionsG = result 

改为

this._getService.getPosts(this.urlG).subscribe(result => {
    this.actionsG = result;
    this.aCount = this.actionsG.actionList[0].count; 
}, error=> console.error('Error: ' + error), ()=> console.log('finish! ActionsG'));