gibbypoo
gibbypoo

Reputation: 55

Typescript/Angular - Call separate function after modal result return

I'm having an issue calling a separate function when my promise is returned by closing or canceling a modal in Angular/Typescript. Perhaps I'm trying to do something impossible but all the examples I've seen either log the returned data back to console or to a variable or throws up an alert. Like so:

modalInstance.result.then(function (result) {
    console.log(result);
});

What I'm looking to do is call a separate function once that result is returned, like:

modalInstance.result.then(function (result) {
    console.log(result);
    this.EditWidget(result);
});

But this doesn't work and I can't seem to figure out why. I've tried about everything and I guess I'm just missing something about how the promise works here.

Any ideas?

Upvotes: 1

Views: 720

Answers (1)

JB Nizet
JB Nizet

Reputation: 691765

My gess is that this is not what you expect it to be. You need to capture the value of this and use the captured value in the callback:

var that = this;
modalInstance.result.then(function (result) {
    console.log(result);
    that.EditWidget(result);
});

or to bind the function to this:

var callback = function (result) {
    console.log(result);
    this.EditWidget(result);
};

modalInstance.result.then(callback.bind(this));

Upvotes: 1

Related Questions