【发布时间】:2016-11-02 20:13:09
【问题描述】:
花时间弄清楚为什么会发生这种情况。我已经使用 CLI 设置了一个 A2 项目,并且我有以下组件:
import { Component, OnInit } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
@Component({
selector: 'app-user-profile-card',
templateUrl: 'user-profile-card.component.html',
styleUrls: ['user-profile-card.component.scss']
})
export class UserProfileCardComponent implements OnInit {
public person;
constructor(private http: Http) {
}
getUserProfile(){
this.http.get('http://somedomain.com/12345')
.map(res => res.json())
.subscribe(
person => { this.person = person.person },
err => console.error(err),
() => console.log(this.person.sex)
);
}
ngOnInit() {
this.getUserProfile();
}
}
this.person.sex 的控制台日志显示正确的值,但是当我尝试从模板绑定到它时:
<div>{{ person.sex }}</div>
我收到以下错误:
undefined 不是对象(评估 'self.context.person.sex')
对此有何想法?
【问题讨论】:
标签: http angular typescript get angular2-template