繁体   English   中英

Angular2:从http.get响应中提取数组元素

[英]Angular2: extract array element form http.get response

Angular2非常有趣! (讽刺的结尾)

这个特定的对象成功地从数组中提取了一个非常特定的元素,然后能够将其显示在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捕获此响应并将其转换为变量的原因。 我认为这不应该那么困难吗?

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.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM