3

I have the following controller which works fine:

function Controller() {}

Controller.prototype = {
    getResult: function(project) {
        var that = this;

        jQuery.ajax({
            async: false,
            url: "/my-service/call?project=" + project,
            dataType: "json",
            success: function(data) { 
                that.result = data;
            }
        });
    }
};

I'd like to use AngularJS .scope.$bind to see if I could eliminate the 'var that = this;' hack. But the following doesn't work:

function Controller() {}

Controller.prototype = {
    getResult: function(project) {
        angular.scope.$bind(jQuery.ajax({
            async: false,
            url: "/my-service/call?project=" + project,
            dataType: "json",
            success: function(data) { 
                this.result = data;
            }
        }))();
    }
};

What am I missing?

2
  • Please don't ask the same question multiple times. Thanks. Commented Jul 13, 2011 at 2:51
  • @Graham The user in this question is asking about angular.scope as a programming construct, not as the name of the library. Tags are meant to be used for searchability. Commented Aug 18, 2017 at 18:24

1 Answer 1

2

Misko Hevery on the angular mailing responded with:

Controller.prototype = {
    getStuff: function(project) {
        jQuery.ajax({
                    async: false,
                    url: "/service/get-stuff",
                    dataType: "json",
                    success: angular.bind(this, function(data) {
                        this.stuff = data;
                    })
                });
    }
};

He also suggested using angular.service.$xhr instead of jQuery.ajax.

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.