0

Angular2 太有趣了!(讽刺结束)

这个特殊的plunker成功地从数组中提取了一个非常特定的元素,然后能够将其显示到 templateUrl 中。一般来说,在模板中显示数据确实很容易。

不幸的是,这不是我的问题。我需要做的是从 dataService / Injectable 获取一个值并将其转换为一个变量,然后将该变量传递给图表程序。基本上,我需要这样的东西: console.log(last);

   //a simple json data component
   import {Component, View} from 'angular2/angular2'
   import {DataService} from './dataService'

   @Component({
    selector: 'my-data',
  templateUrl: 'src/template.html'
})
export class Data {
  constructor(dataService:DataService) {
    dataService.dataObser
      .subscribe(dataObj => {
        this.last = dataObj.person.last;
        this.dataObj = dataObj;
      });
      console.log("this is what I'm looking for " + this.last);
  }
}

然后,我将获取变量并传递给 chartData,如本问题所述。这就是为什么我需要从 json 中捕获此响应并将其转换为变量的原因。我想这应该没有那么难吧?

4

1 回答 1

0
dataService.dataObser
  .subscribe(dataObj => {
    this.last = dataObj.person.last;
    this.dataObj = dataObj;
  });
  // this line is executed before the first `dataObject` event arrives.
  console.log("this is what I'm looking for " + this.last);

如果只有一个事件需要在.subscribe()回调中移动这一行,否则添加一个complete回调

dataService.dataObser
  .subscribe(dataObj => {
    this.last = dataObj.person.last;
    this.dataObj = dataObj;
  }, 
  (_) => {}, 
  () => {
    console.log("this is what I'm looking for " + this.last);
  });
  // this line is executed before the first `dataObject` event arrives.
于 2016-02-01T19:36:39.650 回答