angularjs factory $ http多个方法返回相同的数据集

时间:2014-02-23 22:01:45

标签: angularjs

我有一个有4种方法的工厂,但是当我调用其中任何一种方法时,我总是从第一个方法中返回数据集。

工厂代码:

app.factory('EnumLookupsSvc', function ($http) {
var factory = {};

var promise;

factory.getInvoiceTypes = function() {
    if (!promise) {
        // $http returns a promise, which has a then function, which also returns a promise
        promise = $http.get('/api/invoice/GetInvoiceTypes').then(function(response) {
            // The then function here is an opportunity to modify the response
            console.log(response);
            // The return value gets picked up by the then in the controller.
            return response.data;
        });
    }
    // Return the promise to the controller
    return promise;
};

    factory.getLtlClasses = function() {
        if (!promise) {
            // $http returns a promise, which has a then function, which also returns a promise
            promise = $http.get('/api/invoice/GetLtlClasses').then(function(response) {
                // The then function here is an opportunity to modify the response
                console.log(response);
                // The return value gets picked up by the then in the controller.
                return response.data;
            });
        }
        // Return the promise to the controller
        return promise;
    };

    factory.getDirections = function() {
        if (!promise) {
            // $http returns a promise, which has a then function, which also returns a promise
            promise = $http.get('/api/invoice/GetDirections').then(function(response) {
                // The then function here is an opportunity to modify the response
                console.log(response);
                // The return value gets picked up by the then in the controller.
                return response.data;
            });
        }
        // Return the promise to the controller
        return promise;
    };

    factory.getLineItemTypes = function() {
        if (!promise) {
            // $http returns a promise, which has a then function, which also returns a promise
            promise = $http.get('/api/invoice/GetLineItemTypes').then(function(response) {
                // The then function here is an opportunity to modify the response
                console.log(response);
                // The return value gets picked up by the then in the controller.
                return response.data;
            });
        }
        // Return the promise to the controller
        return promise;
    };


return factory;
});

控制器代码:

"use strict";

app.controller('InvoiceDetailEditorCtrl', function ($scope, $routeParams, InvoiceSvc, EnumLookupsSvc) {

EnumLookupsSvc.getLtlClasses().then(function (data) {
    $scope.ltlClasses = data;
});

EnumLookupsSvc.getDirections().then(function (data) {
    $scope.directions = data;
});

EnumLookupsSvc.getLineItemTypes().then(function (data) {
    $scope.lineItemTypes = data;
});

EnumLookupsSvc.getInvoiceTypes().then(function (data) {
    $scope.invoiceTypes = data;
});

$scope.invoice = InvoiceSvc.get({ id: $routeParams.invoiceId });



});

Chromes网络监视器显示只有一个请求发送到/ api / invoice / GetLtlClasses

FIXED! 的 * 修复方法是将var promise转移到方法中: *

factory.getInvoiceTypes = function () {
    var promise;
    if (!promise) {
        // $http returns a promise, which has a then function, which also returns a promise
        promise = $http.get('/api/invoice/GetInvoiceTypes').then(function(response) {
            // The then function here is an opportunity to modify the response
            console.log(response);
            // The return value gets picked up by the then in the controller.
            return response.data;
        });
    }
    // Return the promise to the controller
    return promise;
};

1 个答案:

答案 0 :(得分:3)

这是因为你创建了promise全局变量,所以第一次调用将它设置为某个值,并将其设置为所有其他调用!promise为false,它们返回promise。