Angular 2订阅共享项目到多个vars

时间:2016-10-24 04:53:34

标签: angular

我想将数据分享到两个变量,项目和标题,我该如何实现?

items = [];
title = [];

ngOnInit() {
    this.dataService.fetchData(this.id)
        .subscribe(
        (data) => this.items = data,
        (error) => handleError(error),
    );
}

2 个答案:

答案 0 :(得分:4)

ngOnInit() {
    this.dataService.fetchData(this.id)
        .subscribe(
        (data) => {
          this.items = data;
          this.title = data;
        },
        (error) => handleError(error),
    );
}

答案 1 :(得分:3)

this.dataService.fetchData(this.id)
  .subscribe(
    (data) => this.items = this.title = data,
    (error) => handleError(error),
);