2016-09-18 98 views
1

广东话访问深层次的对象,我从存储到一个组件变量HTTP响应在HTTP响应

{ 
"files": [ ], 
"posts": [ 
    { 
     "content": "Dies ist ein Test-Posting, das zum Projekt 2 gehört.", 
     "id": 2, 
     "name": "Beispiel Post 2", 
     "timestamp": "Wed, 10 Aug 2016 19:52:09 GMT" 
    } 
], 
"project": { 
    "id": 2, 
    "info": "Dies ist der Text für das Beispiel Projekt 2", 
    "name": "Beispiel Projekt 2", 
    "timestamp": "Wed, 10 Aug 2016 19:50:59 GMT" 
    } 
} 

它得到这个数据,现在我想访问它在我的模板。我试图做这样的事情:

{{project.project.id}} 

它不工作。对象存储这样的:

Object { files: Array[0], posts: Array[1], project: Object } 

我已经尝试了这样的解决方案:iteration a json object on Ngfor in angular 2,但它不会帮助。

继承人的例外,我得到了:
Uncaught (in promise): Error: Error in ./ProjectDetailComponent class ProjectDetailComponent - inline template:0:4 caused by: self.context.project is undefined

有关详细信息,这里有文件:

组件:

@Component({ 
    selector: 'app-project-detail', 
    templateUrl: './project-detail.component.html', 
    styleUrls: ['./project-detail.component.sass'] 
}) 
    export class ProjectDetailComponent implements OnInit { 
    private project; 
    private errorMessage; 

    constructor(private route: ActivatedRoute, 
       private router: Router, 
       private projectService: ProjectsService) { 
    } 

    ngOnInit() { 
     this.route.params.forEach((params: Params) => { 
      let id = +params['id']; 
      this.projectService.getProjects(id).subscribe(
       function (project) { 
        this.project = project; 
       }, 
       error => this.errorMessage = <any>error 
      ); 
     }); 
    } 

} 

服务:

@Injectable() 
export class ProjectsService { 

    constructor(private http: Http) {} 

    private projectsUrl = 'http://localhost:2000/api/projects/'; 

    getProjects(id?: number): Observable<any>{ 
     if(id){ 
      return this.http.get(this.projectsUrl + ""+id) 
       .map(this.extractData) 
       .catch(this.handleError); 
     } else { 
      return this.http.get(this.projectsUrl) 
       .map(this.extractData) 
       .catch(this.handleError); 
     } 


    } 

    private extractData(res: Response) { 
     let body = res.json(); 
     return body; 
    } 
    private handleError (error: any) { 
     // In a real world app, we might use a remote logging infrastructure 
     // We'd also dig deeper into the error to get a better message 
     let errMsg = (error.message) ? error.message : 
      error.status ? `${error.status} - ${error.statusText}` : 'Server error'; 
     console.error(errMsg); // log to console instead 
     return Observable.throw(errMsg); 
    } 
} 

我欣赏任何帮助!

+0

“它不工作”是不是发生了什么;-) –

回答

1

该值未分配给您的事情。

ngOnInit() { 
    this.route.params.forEach((params: Params) => { 
     let id = +params['id']; 
     this.projectService.getProjects(id).subscribe(

      // function (project) { 
      // should be 
      (project) => {    // <<<<=== change 
       this.project = project; 
      }, 
      error => this.errorMessage = <any>error 
     ); 
    }); 
} 

,并用它来与

{{project?.project?.id}} 
+0

遗憾的是没有一个很详细的说明。我认为'elvis'操作员只是检查变量是否被定义。纠正我,如果我错了。 – Lelsoos

+0

在继续评估其余部分之前,它检查'?.'左边的表达式是否为空。很难说这是否是问题,您的情况是没有关于问题的更多细节。 –

+0

我认为这个问题看起来很简单。我将响应存储为变量中的对象。现在我想在模板中插入该变量,但它不起作用。它不会在渲染模板中显示任何内容。 – Lelsoos