2017-09-18 71 views
0
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); 
     }); 
} 

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

回答

0

您可以使用箭头功能:

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

箭头功能捕捉其中函数创建这样this将指向FloorPlanComponent实例this

+0

感谢亚历山大快速回复。我还有一个场景被卡住了。见下面的代码。 drag:any = d3.drag()。on(“drag”,this.dragmove);在dragmove(d)函数中,我希望将其作为平面布局组件以及获取所选拖动项目的此对象。这怎么可能。 –

+0

你可以在局部变量中保存指向'this'的指针并使用闭包来访问它: 'let self = this; drag:any = d3.drag()。on(“drag”,function(){ let dragItem = this; self.dragmove(dragItem); });' –

+0

它不起作用。我用self:any = this; (在类的根级别)。在函数内部,自身仍然指窗口对象。 –

0

如何

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

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

我使用了相同的方法,但是使用这种方法我无法使用我的服务。它显示为未定义。 –

+0

你可以从构造函数注入你的服务,不是吗? –

相关问题