0

I am having a callback function in angular like below.

blob= "(function(params1, params2, this.clientCallback) {})();";
serverBundle = eval(blob); // evaluating the above received bundle file as a blob.

clientCallback() { } //unable to access any thing which is outside this block. like.. 'this', 'httpClient' etc.. 

// calling above evaluated method and passing the callback method here.
serverBundle(params1, params2, this.clientCallback);

trying to understand that how can I access the angular scope inside 'clientCallback()' method?

Please help. Thanks!

  • We need to see more of your code... – CptKicks Nov 20 '19 at 12:38
  • 1
    if this is angular and not angularjs, you should use `NgZone` to run inside of the scope of angular, check `run` function of `NgZone` and also, I hope that you know that eval == evil, you really need to know that the usage of eval can be a deep security hole. – dlcardozo Nov 20 '19 at 12:38
  • Ahh! got it resolved. Just bind 'this' with callback method(this.clientCallback) while using like below. `serverBundle(params1, params2, this.clientCallback.bind(this));` Thanks for the help :) If anyone of you have angular way of doing this? – Madhav Gupta Nov 20 '19 at 12:42
  • `let self = this; clientCallback() { self.serverBundle(params1, params2, this.clientCallback); }` or use a arrow function instead of regular function which is recommended – Joel Joseph Nov 20 '19 at 12:45
  • Hi @dlcardozo, yes. you are correct. Can you suggest any other way of evaluating a JS blob file besides using `eval()` ? Thanks for mentioning :) – Madhav Gupta Nov 20 '19 at 12:47

0 Answers0