0

after this..

 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(p => this.person = p);
});

}

why does

console.log(this.person);

return undefined? code my code is based on this It all works fine when I use it in the html, for example

{{person.id}} 

and it works fine in my component.ts when I pass the data to a Material dialog using

data:this.activity

I've added

@input person:Person;

What do I need to do to be able to console.log(this.person) ?

The reason I'm asking is curiosity and also I need use patchValue on a reactive form and I get undefined if I add

 this.personForm
       // .patchValue({name:this.person.name});

I rewrote the subscribe line

         .subscribe((response) =>{
          this.person = response; 
          this.personForm
         .patchValue({name:this.person.name});              
          }); 

and it works but probably not the nicest way to do it?

vtechmonkey
  • 123
  • 2
  • 3
  • 11
  • Where is `console.log(this.person);` line? – eko Jul 12 '17 at 10:38
  • 2
    Possible duplicate of [How do I return the response from an Observable/http/async call in angular2?](https://stackoverflow.com/questions/43055706/how-do-i-return-the-response-from-an-observable-http-async-call-in-angular2) – eko Jul 12 '17 at 10:41
  • Not enough context to establish what is going on, try moving your logic into ngAfterViewInit() instead of ngOnInit(). Maybe your data is not ready during ngOnInit() – adentum Jul 12 '17 at 10:46
  • @echonax thanks that is pretty much it – vtechmonkey Jul 12 '17 at 12:29
  • @echonax I had it after `this.peopleService .get(id) .subscribe(p => this.person = p);` – vtechmonkey Jul 12 '17 at 12:34
  • @vtechmonkey if the issue is solved can you mark it as solved please: https://meta.stackexchange.com/questions/250981/new-ui-encourages-askers-to-confirm-or-dispute-duplicate-votes – eko Jul 12 '17 at 12:39
  • @echonax sure, where!? – vtechmonkey Jul 12 '17 at 12:47
  • https://meta.stackexchange.com/questions/250981/new-ui-encourages-askers-to-confirm-or-dispute-duplicate-votes – eko Jul 12 '17 at 12:49
  • yeah I read that but the banner is gone – vtechmonkey Jul 12 '17 at 12:50
  • It's gone cuz you edited the question :-) it will reappear when another person also mark it as dupe – eko Jul 12 '17 at 12:53

0 Answers0