我试图通过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",
...
答案 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'));