angular2 - service返回订阅中的observable

时间:2017-01-31 11:50:25

标签: angular typescript ionic2

我有一个问题。 我的服务返回一个有更多数组的对象,我发送navparams为我的页面和订阅,但在我的页面返回observable,我不使用observable。

如何解决这个问题?

服务

getInfoAccountById(id: any) {
    this.module = 'accounts/' + id + '/infos';
    let headers = new Headers();
    headers.append(this.key, this.token);

    return this.http.get(this.url + this.module, {
        headers
    }).map(res => res.json().Result)


}

主页nav params

constructor(public navCtrl: NavController, public navParams: NavParams, public acc: AccountService) {
  this.navParams = navParams;


  this.dataDetails = {
      id: this.navParams.get('data').Id,
      name: this.navParams.get('data').Identification,
      complement: this.navParams.get('data').Complement,
      account: this.navParams.get('data').AccountType.Identification,
      pricelist: this.navParams.get('data').PriceList,
      active: this.navParams.get('data').Active,
      color:this.acc.getdetailsAccountById(this.navParams.get('data').Id).subscribe(data => { this.datad = data.Color; }),
          informations:this.acc.getInfoAccountById(this.navParams.get('data').Id) // this line have a problem



      };


  }

辅助页面接收了参数

constructor(public navCtrl: NavController, public navParams: NavParams, public acc: AccountService, public http: Http) {

    this.id = this.navParams.data.id;
    this.name = this.navParams.data.name;
    this.complement = this.navParams.data.complement;
    this.account = this.navParams.data.account;
    this.pricelist = this.navParams.data.pricelist;
    this.active = this.navParams.data.active;
    this.color = this.navParams.data.color;
    this.infos = this.navParams.data.informations;
    console.log(this.infos);

}

1 个答案:

答案 0 :(得分:0)

您必须在订阅中进行设置。

constructor(public navCtrl: NavController, public navParams: NavParams, public acc: AccountService) {
  this.navParams = navParams;


  this.dataDetails = {
      id: this.navParams.get('data').Id,
      name: this.navParams.get('data').Identification,
      complement: this.navParams.get('data').Complement,
      account: this.navParams.get('data').AccountType.Identification,
      pricelist: this.navParams.get('data').PriceList,
      active: this.navParams.get('data').Active,
      color:'',
      information:''
      };
this.acc.getdetailsAccountById(this.navParams.get('data').Id).subscribe(data => { this.dataDetails.color = data.Color; })
this.acc.getInfoAccountById(this.navParams.get('data').Id).subscribe(data=> this.dataDetails.information = data;
  }