javascript Angular,从当前服务调用服务函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16838581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:11:29  来源:igfitidea点击:

Angular, call service function from current service

javascriptangularjs

提问by Simcha

I have service called "sharedData" with some functions, how to call one of these functions from another such functions? here the code(marked trouble functions with "???????"): Thanks

我有一些名为“sharedData”的服务,有一些函数,如何从另一个这样的函数调用这些函数之一?这里的代码(用“???????”标记故障功能):谢谢

service('sharedData', function ($http) {
    var refillList = [];
    var orderCart = {
        orderPlace: null,
        orderList: [],
        totalSum: 0
    };

    return {
        ....
        addRefill: function(value) {
           ...here some logic....
        },

        addOrder: function(order) {
            ...here some logic....
        },
        sendOrder: function(order, refill) {
            $http.get(config.urls.ajaxOrder + "{\"order\":{\"table_id\":" + orderCart.orderPlace + ",\"item_id\":" + order.id + ",\"amount\":1,\"action\":1}}").success(function(dataDetails) {
                if (dataDetails.success) {
                    if (refill == 1) {
                        // Filling refill list
                        ??????????????????this.addRefill(order);?????????
                    }
                    // Filling order cart
                    ?????????this.addOrder(order);?????????????
                }
            });
        }
    };
}).

回答by bniwredyc

You should save reference to this.

您应该保存对this.

var self = this;is a common practice.

var self = this;是一种普遍的做法。

sendOrder: function(order, refill) {
    var self = this;
    $http.get(config.urls.ajaxOrder + "{\"order\":{\"table_id\":" + orderCart.orderPlace + ",\"item_id\":" + order.id + ",\"amount\":1,\"action\":1}}")
        .success(function(dataDetails) {
            if (dataDetails.success) {
                if (refill == 1) {
                    // Filling refill list
                    self.addRefill(order);
                }
                    // Filling order cart
                    self.addOrder(order);
                }
            }
        });
    }

Update 2016

2016 年更新

Now, with ES6 you can use arrow functions like this:

现在,在 ES6 中,您可以像这样使用箭头函数:

sendOrder: function(order, refill) {
    $http.get(config.urls.ajaxOrder + "{\"order\":{\"table_id\":" + orderCart.orderPlace + ",\"item_id\":" + order.id + ",\"amount\":1,\"action\":1}}")
        .success(dataDetails => {
            if (dataDetails.success) {
                if (refill == 1) {
                    // Filling refill list
                    this.addRefill(order);
                }
                    // Filling order cart
                    this.addOrder(order);
                }
            }
        });
    }

Arrow functions doesn't change a context, so thiswill be the same this.

箭头函数不会改变上下文,所以this将是相同的this

MDN article about arrow functions

关于箭头函数的 MDN 文章

回答by Paul Ryan

The problem is that this in this callback function refers to the parent container of the callback which is $http in this case. What you'll want to do is create an instance of the parent object outside of the callback and reference that from within the callback.

问题是这个回调函数中的 this 指的是回调的父容器,在这种情况下是 $http 。您要做的是在回调外部创建父对象的实例,并从回调内部引用该实例。

Something like:

就像是:

....
{
    ....
    addRefill: function(value) {
       ...here some logic....
    },

    addOrder: function(order) {
        ...here some logic....
    },
    sendOrder: function(order, refill) {
        var rootObj = this;
        $http.get(config.urls.ajaxOrder + "{\"order\":{\"table_id\":" + orderCart.orderPlace + ",\"item_id\":" + order.id + ",\"amount\":1,\"action\":1}}").success(function(dataDetails) {
            if (dataDetails.success) {
                if (refill == 1) {
                    // Filling refill list
                    rootObj.addRefill(order);
                }
                // Filling order cart
                rootObj.addOrder(order);
            }
        });
    }
};
....

This is of course just a solution but the main concept to keep in mind is that the function is being called from the success promise not from your object.

这当然只是一个解决方案,但要记住的主要概念是该函数是从成功承诺中调用的,而不是从您的对象中调用的。