0

I have a function exposed on window object. This is getting invoked from my Windows Application.

window.patchTransaction = this.AngularService.myMethod.bind(this);

In AngularService.js

myMethod(val) {
window.alert(val); // argument i am passing from Windows appication
 window.alert(this); // undefined
}

How can i get this reference of AngularService inside myMethod?

10
  • What is the value of "this" when you call window.patchTransaction = this.AngularService.myMethod.bind(this) ? Commented Jan 17, 2018 at 15:07
  • It's a string which i am passing from Windows Application. Commented Jan 17, 2018 at 15:09
  • No - the value you're passing from Windows application will be "val", not "this". The "this" value will be whatever "this" is when you call .bind. Commented Jan 17, 2018 at 15:12
  • Inside of the function this is undefined which is the problem. I have other properties of this which i need to access. Commented Jan 17, 2018 at 15:12
  • Right. Which leads me back to my original question: when you call window.patchTransaction = ..., what is the value of "this"? (I'm not asking about the value of "this" inside myMethod. I'm asking what it is when you setup window.patchTransaction.) Commented Jan 17, 2018 at 15:13

1 Answer 1

1

Rather than directly assigning Angular Service method to exposed window object.

In AngularController

window.patchTransaction = this.delegateMethod.bind(this);

delegateMethod(str) {
 this.AngularService.myMethod(str);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.