1

我是angular2的新手,尝试将对象结果绑定到html模板。我能够通过使用Observable从srtvice获取数据。我能够在控制台和模板中看到JSON数据。但是,当我尝试从结果json访问属性时,它不会将数据显示为下面的图像。如何在Angular2中打印模板中的对象属性

Output

下面是我认为的组元代码,

import { Component, OnInit } from '@angular/core'; 
import { ActivatedRoute } from '@angular/router'; 

import { JuniorService } from '../Service/service.junior'; 
import { Employee } from '../../models/Employee'; 


@Component({ 
    selector: 'my-getjunior', 
    template: `<h1>Details of {{id}} 
    <hr/> 
    JSON : {{Junior | json}} 
    <hr/> 
    Summary : {{junior?.summary}}</h1>` 
    //templateUrl: './app/Junior/get/junior.get.html' 
}) 
export class JuniorGet implements OnInit 
{ 

errorMessage: string; 
Junior: Employee; 
id: number; 
private sub: any; 

    constructor (private juniorService: JuniorService, private route: ActivatedRoute) { 

} 

    ngOnInit(){ 
     this.sub = this.route.params.subscribe(params => { 
     this.id = +params['id']; // (+) converts string 'id' to a number 
    this.getJunior(this.id); 
    }); 
    } 

    getJunior(id) { 
     this.juniorService.getById(id) 
         .subscribe(
         data => { 
          this.Junior = data; 
          console.log("Junior data", this.Junior); 
          }, 
         error => this.errorMessage = <any>error); 
    } 


} 

我呼吁IMIT方法和数据我想在模板中看到的服务。

还有哪些地方需要更改打印数据?

回答

1

使用这样的代码

{{Junior?.summary}} 

there is small typo in your code you are using junior instead of Junior

,而不是在控制台thorwing任何错误,因为你已经在你的代码中使用safe navigation (?)

+1

它的工作与你的解决方案:),但是我想同样的事情在30分钟之前,它不工作(不知道是什么问题)。 感谢您的回答 –

+0

你可以快速回答下面的错误的绑定属性与文本框在双向?,

+0

这是模板错误 –

相关问题