在TypeScript中,创建具有http构造函数的同一类的对象

时间:2017-09-18 04:35:30

标签: typescript angular2-services

 export class FloorPlanComponent implements AfterViewInit, OnInit {
      constructor();
      constructor(public _baseComponentService?: BaseComponentService,
                  public _internalZoneService?: InternalZoneService,
                  public routeParams?: ActivatedRoute,
                  public internalAssetService?: InternalAssetService,
                  public slimLoadingBarService?: SlimLoadingBarService,
                  private errorLoggerService?: ErrorLoggerService
                 ) {
                     if (this._baseComponentService != null) {
                           this._baseComponentService.SetPageTitle("Zones - Internal");
                   }
      this.imgDelZone = parentElement.append("image")
        .attr("id", "delZone" + newId)
        .attr("idCounter", newId)
        .attr("r", "NaN")
        .attr("stroke-width", 0)
        .attr("opacity", 0.8)
        .attr("x", function (d) { return d.x + (shapeWidth - 22); })
        .attr("y", function (d) { return d.y + 1; })
        .attr("style", "opacity: 1; stroke-width: 1;")
        .attr("xlink:href", "../app/assets/images/Remove_Delete-22.png")
        .attr("preserveAspectRatio", "none")
        .attr("transform", " ")
        .attr("height", 18)
        .attr("width", 18)
        .attr("cursor", "pointer").on("click", function () {
            var comp = new FloorPlanComponent();
            comp.promptDeleteZone(this);
        });
}

在“点击”功能中,我想用它来引用我的课程。但它将此称为窗口对象。为了解决这个问题,我在无参数构造函数的帮助下创建了一个类的对象。但据此,我无法获得任何服务。请帮忙。

2 个答案:

答案 0 :(得分:0)

您可以使用箭头功能:

  attr("cursor", "pointer").on("click", () => {
    ...
  });

箭头函数捕获创建函数的this,因此this将指向FloorPlanComponent实例。

答案 1 :(得分:0)

怎么样

.attr("cursor", "pointer").on("click", this.onClicked.bind(this));

.............
onClicked() {
    var comp = new FloorPlanComponent();
    comp.promptDeleteZone(this);
}