簡體   English   中英

如何使用 AngularJS scope.$bind

[英]How to use AngularJS scope.$bind

我有以下 controller 工作正常:

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

我錯過了什么?

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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM