2017-03-11 4 views
0
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のtypecriptオブジェクトにマップしたいだけです。これどうやってするの。それは '応答'の値を取得しています。しかし、personオブジェクトにはマッピングできません。私はあなたのコンポーネントのメソッドでは例えば、あなたがあなたの応答を使用したい場合は、結果/Map httpリクエストはAngular2オブジェクトにアクセスし、外部からアクセスします

+0

あなたはここであなたのJSONレスポンスを共有することができます! – Viraj

+0

**外部からアクセス**外部からはどういう意味ですか? – Aravind

+0

ngOnInit()メソッドの範囲外 – rdanusha

答えて

0

.subscribe(response => this.person = .json().data as Person); // get response value

からの応答データを使用したいです、最も簡単な方法は、あなたが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! 
} 
+0

jsonデータをpersonオブジェクトにマップして外部から使用したい – rdanusha

0

をキャッシュするためにあなたのget使用.publishLast().refCount()に外

あなた PeopleService
+0

いずれかの回答がありましたか? :) – Alex