服务功能内的角度呼叫服务功能,参考此不起作用

时间:2014-10-03 17:13:36

标签: javascript angularjs

我试图在同一服务的另一个功能中调用服务功能,但看到一些奇怪的行为。希望这是我忽略的一些明显的错误。这是我服务的相关部分:

app.factory('Data', ['$http', function($http) {

        var Data = this;
        var theProduct = {};
        var seletedSku = {};
        var uniqueItem = {};

        return {

                product: function(){
                        return theProduct;
                },

                getProduct: function(ext_id){

                        console.log(Data.product);
                        console.log(this.product);

                },
        }
}]);

正如您所看到的,在getProduct()函数中,我只是尝试记录product函数,以确认引用是否正常工作。调用getProduct()时,第一行记录undefined,第二行记录我期望的product函数:

function (){
    return theProduct;
}

为什么我的参考不起作用?您可以在服务的顶部看到我将this保存到Data变量。有什么想法吗?

我粘贴下面的完整服务代码仅供参考,以防万一:

app.factory('Data', ['$http', function($http) {

    var Data = this;
    var theProduct = {};
    var seletedSku = {};
    var uniqueItem = {};

    return {
        //return the current product being used in the app
        product: function(){
            return theProduct;
        },

        //get a products data from the server and set it as the current product being used by the app
        getProduct: function(ext_id){

            console.log(Data.product);
            console.log(this.product);

            return $http.post('get_product', {product_id: ext_id}).success(function(data){
                theProduct = data;

                //when a product is fetched, set the app's unique item
                if(theProduct.unique_item){
                    Data.setUniqueItem(theProduct.unique_item);
                }
                else{
                    Data.setUniqueItem({});
                }
            });

        },

        //change the currently selected sku for the app
        setSku: function(sku){

            if(sku){
                selectedSku = sku;
            }
            else{
                //null was passed, meaning, the -- Selected SKU -- option
                //was chosen, so reset selectedSku back to an empty object
                selectedSku = {};
            }

            //when sku is set, also need to set current unique item
            if(selectedSku.unique_item){
                Data.setUniqueItem(selectedSku.unique_item);
            }
            else{
                Data.setUniqueItem({});
            }

            return selectedSku;
        },

        //get the currently selected sku for the app
        sku: function(){
            return selectedSku;
        },

        //set the current unique item
        setUniqueItem: function(item){

            //before set a unique item, we need to check if the unique
            //item we are setting is the same as the current unique item.
            //if it is, we need to set them equal so the new item reflects
            //current state of it since it's not repulling the item from the database
            if(item.id != uniqueItem.id){
                //only change the unique item if they are not the same
                //if they are the same, just leave unique item as is
                uniqueItem = item;
            }
            console.log(uniqueItem);
            return uniqueItem;
        },

        //get the current unque item
        uniqueItem: function(){
            return uniqueItem;
        }
    }
}]);

1 个答案:

答案 0 :(得分:1)

因为在引用时,this没有自身的上下文作为对象文字。