0

I'm trying to modify a variable inside a function, but there must be something with the scope that I'm not understanding, code:

app.factory("UserService", function(){
var foo = 'bar';
return {
    init: function(){
        var self = this;
        self.create();
    },
    create: function(){
        $.post(server+"api/users", { 
                uuid: self.uuid 
            })
            .done(function(data) {
                if(data.reply !== 0){ 
                    foo = true;
                } else {
                    foo = false;
                }

            });
            return foo;                
        }
    };
});

Why can't I change the value of foo from within the functions? I've tried doing this.foo, self.foo, after assigning this to self, among so many other things, and I believe I'm missing something way too obvious and I'll feel like an idiot after someone clears it up for me.

Thanks.

3
  • 1
    You can. What you can't do is pretend $.post isn't async and try to return it from an async function. Commented Feb 18, 2015 at 20:51
  • How do I get around that? I need to know if the create() function gets executed properly or not, can I simply change foo inside the function and later down the line check the value of foo, without returning anything? Commented Feb 18, 2015 at 20:53
  • You have to return a promise. The issue isn't about scope or context. Commented Jan 13, 2016 at 20:00

2 Answers 2

1

By looking at your code you need to first change $.post to $http.post to get better controller over binding by executing $digest cycle after its execution, Using jQuery inside angular would create more problem with scope digest cycle.

Your code will not work because you made an Ajax from the code and returning variable from outside of done, It should be return from the completion of promise using done or success method.

Service

app.factory("UserService", function($http) {
    var foo = 'bar';
    return {
        init: function() {
            var self = this;
            self.create();
        },
        create: function() {
            return $http.post(server + "api/users", {
                    uuid: self.uuid
                })
                .then(function(res) {
                    if (res.data.reply !== 0) {
                        return foo = true; //returning data when promise is resolved
                    } else {
                        return foo = false; //returning data when promise is resolved
                    }

                });
        }
    };
});

Hope this could help you, Thanks.

Sign up to request clarification or add additional context in comments.

6 Comments

if I assign the function to a variable, eg; r = self.create(); and then console.log(r); I get [object Object]
if you want to get result then from create() do self.create().then(function(data){ console.log(data);//here you will get value of foo inside data}) ..Use then to resolve chain promise because create method return a promise object..
I'm having a similar issue in my own factory code, and noticed that the OP never signed off on this post. Why is that ?
Yes indeed, but through another post that someone assisted with - i.e. stackoverflow.com/questions/32406163/…
@PankajParkar - thank you anyway for taking the time to response.
|
0

Try that:

app.factory("UserService", function(){
var foo = 'bar';
return {
    init: function(){
        var self = this;
        self.create();
    },
    create: function(){
        $.post(server+"api/users", { 
                uuid: self.uuid 
            })
            .done(function(data) {
                if(data.reply !== 0){ 
                    return true;
                } else {
                    return false;
                }

            });

        }
    };
});

This should return true or false when the asynchronus function is done.

Cheers,

Valentin

1 Comment

Wouldn't the function that called create be long-done by then?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.