Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call angular2 method from javascript function

I am using jquery kendo ui grid and from that edit button i am trying to call angular2 method. My setup is simple:

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
   });
}
}

It's working code the only issue is this. I can call the angular2 method by just stating

 click:this.edit

or

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

but in both case the method is not from the current instance. So in this case i cannot make use of the http service or any local variable or methods inside edit

like image 484
Navin Avatar asked Mar 15 '23 06:03

Navin


1 Answers

Try something like this

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

or

var self = this;
$("#grid").kendoGrid({

   click: function () {
     self.edit();
   });
like image 169
TGH Avatar answered Mar 25 '23 03:03

TGH