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.
}
I'm newbie to Angular2 JS. As above code, I just want to map the json response data to angular2 typescript object. How can I do this. It is getting 'response' value. But can not map to person object. I want to use response data from the outside
.subscribe(response => this.person = .json().data as Person); // get response value
在您PeopleService
您get
使用.publishLast().refCount()
来缓存结果/
If you want to use your response, eg in a method in your component, the easiest way would be to call that method from inside the subscription, after you have got your value to 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!
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.