export class PersonEditDetailsComponent implements OnInit,OnDestroy {
person: Person;
sub: any;
constructor(private peopleService: PeopleService,
private route: ActivatedRoute,
private router: Router) {
}
ngOnInit() {
this.sub = this.route.params.subscribe(params => {
let id = Number.parseInt(params['id']);
console.log('getting person with id: ', id);
this.peopleService
.get(id) // send http request
.subscribe(response => this.person = response); // get response value
});
}
//I want to access this.person values here.
}
我是Angular2 JS的新手。如上面的代码,我只想将json响应数据映射到angular2 typescript对象。我怎样才能做到这一点。它正在获得“回应”价值。但无法映射到人物对象。我想使用外部的响应数据
答案 0 :(得分:0)
.subscribe(response => this.person = .json().data as Person); // get response value
PeopleService
get
使用.publishLast().refCount()
缓存结果/
答案 1 :(得分:0)
如果您想使用您的回复,例如在组件中的方法中,最简单的方法是在您获得person
的价值后,从订阅内部调用该方法:
ngOnInit() {
this.sub = this.route.params.subscribe(params => {
let id = Number.parseInt(params['id']);
console.log('getting person with id: ', id);
this.peopleService
.get(id)
.subscribe(response => {
this.person = response;
this.doSomething(); // call method here!
});
});
}
doSomething() {
console.log(this.person) // here value is available!
}