影响angular2中变量的json属性

影响angular2中变量的json属性,json,angular,Json,Angular,我试图通过Angular 2中的变量影响JSON属性的值,我尝试在我的应用程序组件中访问它,如下所示: export class AppComponent{ count : any; constructor(private _getService : GetService) { this._getService.getPosts(this.urlG) .subscribe( result => t

我试图通过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",
                   ...
被执行之前

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'));

当我这样做并通过以下方式打印aCount的值:
console.log(“aCount:+this.aCount”)我正在获取未定义的值,这意味着我无法通过以下方式访问json文件中的值:this.actionsG.actionList[0].count;当我在html中打印它时,我可以通过以下方式访问它:{{actionList?.count}我不知道如何获取变量的值您需要在
subscribe(…)
中执行,因为此代码是在服务器响应到达时执行的。如果您的代码在
subscribe()
之后,它将在响应到达之前执行,因此未定义。
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'));