2014-02-23 40 views
1

我有一个工厂有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 }); 



}); 

铬合金网络监控器仅示出一个请求到/ API /发票/ GetLtlClasses

FIXED制成! * 此修复程序是VaR的承诺进入方法: *

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

回答

3

这是因为你做出承诺全局变量,因此在第一次调用将其设置为某个值,并且对于所有其他来电!承诺是错误的,他们回复承诺。

相关问题