简体   繁体   English

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

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

Angular2 is so much fun! Angular2非常有趣! (end of Sarcasm) (讽刺的结尾)

This particular plunker is successful in extracting a very specific element from the array and then is able to display it into the templateUrl. 这个特定的对象成功地从数组中提取了一个非常特定的元素,然后能够将其显示在templateUrl中。 Generally speaking is really easy to display data in the template. 一般来说,在模板中显示数据确实很容易。

Unfortunately that is not my issue. 不幸的是,这不是我的问题。 What I need to do is get a value from dataService / Injectable and turn it into a variable and then pass that variable into chart program. 我需要做的是从dataService / Injectable获取值,并将其转换为变量,然后将该变量传递到图表程序中。 Basically, I need something like this: console.log(last); 基本上,我需要这样的东西: 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);
  }
}

I will then take the variable and pass into chartData, as laid out in this question . 然后,我将采用该变量并将其传递到此问题中列出的chartData中。 That is why I need capture this response from the json and turn it into a variable. 这就是为什么我需要从json捕获此响应并将其转换为变量的原因。 I'm think this shouldn't be that difficult? 我认为这不应该那么困难吗?

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);

If there is only one event to be expected move this line inside the .subscribe() callback, otherwise add a complete callback 如果预期只有一个事件,请在.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