如何使用AngularJS范围。$ bind

时间:2022-03-12 19:42:46

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:

我想使用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;
            }
        }))();
    }
};

What am I missing?

我错过了什么?

1 个解决方案

#1


2  

Misko Hevery on the angular mailing responded with:

角度邮件上的Misko Hevery回复:

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.

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

#1


2  

Misko Hevery on the angular mailing responded with:

角度邮件上的Misko Hevery回复:

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.

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