从javascript函数调用angular2方法

Call angular2 method from javascript function

本文关键字:方法 angular2 函数调用 javascript      更新时间:2023-09-26

我使用的是jquery kendo ui网格,通过该编辑按钮,我试图调用angular2方法。我的设置很简单:

export class UserComponent implements OnInit {
     constructor( @Inject(UserService) public userService: UserService,  @Inject(FormBuilder) public fb: FormBuilder) {
...
}

 edit():void{ }
 onInit() {
   $("#grid").kendoGrid({
   ....
   click: function () {
     // Call angular2 method of the current instance
   });
}
}

这是工作代码,唯一的问题是这个。我可以通过说明来调用angular2方法

 click:this.edit

 click: function () {
         UserComponent.prototype.edit()
       });

但在这两种情况下,该方法都不是来自当前实例。因此,在这种情况下,我无法使用http服务或编辑中的任何本地变量或方法

试试这样的

click: function () {
   this.edit();
}).bind(this);

var self = this;
$("#grid").kendoGrid({
   click: function () {
     self.edit();
   });