3

我有以下工作正常的控制器:

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;
            }
        });
    }
};

我想使用 AngularJS .scope.$bind 来查看是否可以消除 'var that = this;' 黑客。但以下不起作用:

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;
            }
        }))();
    }
};

我错过了什么?

4

1 回答 1

2

Misko Hevery 在 Angular 邮件中回复说:

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

他还建议使用 angular.service.$xhr 而不是 jQuery.ajax。

于 2011-07-12T16:32:03.310 回答