从目前的服务角度,呼叫服务功能 [英] Angular, call service function from current service

查看:118
本文介绍了从目前的服务角度,呼叫服务功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务被称为sharedData与某些功能,如何调用从另一个这样的功能,这些功能呢?这里的code(标有???????麻烦功能):谢谢

 服务('sharedData',函数($ HTTP){
    变种refillList = [];
    VAR orderCart = {
        orderPlace:空,
        orderList:[],
        totalSum:0
    };    返回{
        ....
        addRefill:函数(值){
           ...这里一些逻辑....
        },        addOrder:功能(顺序){
            ...这里一些逻辑....
        },
        sendOrder:功能(顺序,笔芯){
            $ http.get(config.urls.ajaxOrder +{\\命令\\:{\\的table_id \\:+ orderCart.orderPlace +,\\ITEM_ID \\:+ order.id +,\\量\\:1,\\动作\\:1}})成功(功能(dataDetails){
                如果(dataDetails.success){
                    如果(笔芯== 1){
                        //灌装填充列表
                        ?????????????????? this.addRefill(订单); ?????????
                    }
                    //灌装订购车
                    ????????? this.addOrder(订单); ?????????????
                }
            });
        }
    };
})。


解决方案

您应该保存参照这个

VAR自我=这一点; 是一种常见的做法。

  sendOrder:功能(顺序,笔芯){
    VAR自我=这一点;
    $ http.get(config.urls.ajaxOrder +{\\命令\\:{\\的table_id \\:+ orderCart.orderPlace +,\\ITEM_ID \\:+ order.id +,\\量\\:1,\\动作\\:1}})
        .success(功能(dataDetails){
            如果(dataDetails.success){
                如果(笔芯== 1){
                    //灌装填充列表
                    self.addRefill(订单);
                }
                    //灌装订购车
                    self.addOrder(订单);
                }
            }
        });
    }

更新2016年

现在,随着ES6你可以用箭头的功能是这样的:

  sendOrder:功能(顺序,笔芯){
    $ http.get(config.urls.ajaxOrder +{\\命令\\:{\\的table_id \\:+ orderCart.orderPlace +,\\ITEM_ID \\:+ order.id +,\\量\\:1,\\动作\\:1}})
        .success(dataDetails => {
            如果(dataDetails.success){
                如果(笔芯== 1){
                    //灌装填充列表
                    this.addRefill(订单);
                }
                    //灌装订购车
                    this.addOrder(订单);
                }
            }
        });
    }

箭头功能不改变的背景下,这样这个将是相同的这个

MDN文章关于箭头功能

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

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

解决方案

You should save reference to this.

var self = this; is a common practice.

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

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

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 this will be the same this.

MDN article about arrow functions

这篇关于从目前的服务角度,呼叫服务功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆