簡體   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