0

I am a bit of a javascript/angular newbie and am having some trouble understanding how the scope works. I have the following code that is not functioning due to scope issues:

Angular:

this.myFxn = function() {
    var x = this.myModel;  //this.myModel is set by an ng-model in the html
    myService.myServiceFxn(x.Id)
        .then(function (response) {
            this.myModel = "";
        });
};

I believe the issue is with my reference to this.myModel inside of the .then(). How can I correctly reference this variable without any scope issues?

Thank you for any help you can give! :)

1 Answer 1

3

Just keep the reference to the controller in a variable like the example with self

this.myFxn = function() {
    var self = this;
    myService.myServiceFxn(self.myModel.Id)
        .then(function (response) {
            self.myModel = "";
        });
};
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.