可以在foreach中使用服务()

时间:2017-02-18 11:27:16

标签: javascript angular angular2-observables vis.js

import { Component, Input, Output, OnInit, OnChanges } from '@angular/core';
import { ViewComponent } from '../view/view.component';
import { HitoService } from '../../services/hito.service';

@Component({
  selector: 'app-time-line',
  templateUrl: './time-line.component.html',
  styleUrls: ['./time-line.component.css'],
  providers: [HitoService]
})

export class TimeLineComponent implements OnChanges, OnInit {
  @Input() calbuscador: String;
  @Input() idcalbuscador: String;
  public pepe: String
  nom_cal1: any;
  hito1: any = {};
  hito2: any = {};

  constructor(public _hitoService: HitoService) {
  }

  ngOnInit() {
  }
  ///we retrieve the value sento to this component in OnChanges
  ngOnChanges() {

    this.nom_cal1 = this.calbuscador;
    this.drawtimeline(this.nom_cal1, this._hitoService, this.idcalbuscador);
  }

  result: any[] = [];
  drawtimeline(nom_cal, _hitoService, idcalbuscador) {

    var container = document.getElementById('timeLine');
    //alert("id cal sele es :" + idcalbuscador);

    var k = 0;
    var j = 0;

    var master = new vis.DataSet();
    var items = new vis.DataSet();

    this.result.push({  "_id": this.idcalbuscador,
      "title": nom_cal });

    this.result.forEach(function (ev) {
      master.add([{ id: ev._id, content: ev.title, cal_id: ev._id }]);
      var g = ev._id;

      for (var i= 0; i<this.result.length; i++){
        console.log("hola");
        console.log(this.result[i]._id);
        this._hitoService.getHitos(this.result[i]._id)
              .subscribe(hito2 => {
                this.hito2 = hito2
                var items: any;
                items = hito2;

          items.forEach(function (item) {
            items.add([{ id: k, group: g, start: item.start_datetime, end: item.end_datetime, style: itemStyle(item.design), className: "pepe" }]);
            k++;
          });
          j++;
        }); 
      }
    });

我正在尝试使用vis.js实现时间轴,我在此类组件中检索时间轴想要的名称和id然后在ngOnChanges我调用函数来绘制传递给它的时间轴时间线的名称,它的id以及其他服务以获取此特定时间线的observables项目。我有一个数组,它将存储我想要查看的时间轴(结果),然后是我订阅的可观察项,以添加时间轴的项目。第一个foreach()将删除结果数组中的第一个元素,获取该结果的项目的可观察项,第二个foreach()将通过observables项目并打印项目,然后重新开始并移动到下一个。但我在浏览器控制台中获得的只有:TypeError: this is undefined。可能没有在forach()

中使用该服务

2 个答案:

答案 0 :(得分:2)

您可以使用forEach中的箭头功能继续使用封闭范围。

&#13;
&#13;
this.result.forEach((ev) => {
  // your code here
}
&#13;
&#13;
&#13;

答案 1 :(得分:1)

将当前this分配给另一个变量(例如,that),然后在回调中使用that

注意:您的回调函数this内部已更改为调用该函数的JavaScript上下文。

const that = this;        // save the current 'this' to 'that' variable

this.result.forEach(function (ev) {
      master.add([{ id: ev._id, content: ev.title, cal_id: ev._id }]);
      var g = ev._id;

      for (var i= 0; i< that.result.length; i++){
        console.log("hola");
        console.log(that.result[i]._id);
        that._hitoService.getHitos(that.result[i]._id)
              .subscribe(hito2 => {
                that.hito2 = hito2
                var items: any;
                items = hito2;

                ....
             ....
           ....  
相关问题