您好我正在开发angularjs中的Web应用程序。我有一个带有两个工厂的java脚本文件,它们对web api进行http调用。我想在另一家工厂内使用一个工厂值输出。以下是我的第一家工厂
myapp.factory('sadadpaymentapi', ['$http', '$cookieStore', 'cfg', 'ScrollFunction', 'leaselisting','$q', function ($http, $cookieStore, cfg, ScrollFunction, leaselisting,$q) {
var sadadpaymentapiobject = {};
var baseurl = cfg.Baseurl;
var LoginID = $cookieStore.get("LoginID");
var cookiePreferredLanguage = $cookieStore.get('PreferredLanguage');
var urlapi = baseurl + "api/ServiceRequest/CreateRSSedad/";
sadadpaymentapiobject.callsadad = function (PaymentType) {
var request = {
url: urlapi,
method: 'POST',
data: {
SRActivityID: LoginID,
PaymentType: PaymentType,
PaymentAmount: "100" //Need to get value from another factory
},
headers: ScrollFunction.getheaders()
};
return $http(request);
}
return sadadpaymentapiobject;
}]);
下面是另一家工厂,我可以在上面的工厂中获得PaymentAmount参数的值。
myapp.factory('leaselisting', ['$http', '$cookieStore', 'cfg', 'ScrollFunction','$q', function ($http, $cookieStore, cfg, ScrollFunction,$q) {
var leaselistingobject = {};
var baseurl = cfg.Baseurl;
var LoginID = $cookieStore.get("LoginID");
var cookiePreferredLanguage = $cookieStore.get('PreferredLanguage');
leaselistingobject.getValue = function () {
var requestObj = {
url: baseurl + "api/ServiceRequest/GetROLSPSRLeaseList/",
method:'POST',
data: {
LoginID: LoginID,
AccountNumber: $cookieStore.get("AccountNumber")
},
headers: ScrollFunction.getheaders()
};
return $http(requestObj).then(function (response) {
return response.data;
});
}
return leaselistingobject;
}]);
当我像PaymentAmount一样注入:leaselisting.getValue()并且如果我尝试在控制台中显示注入值console.log(leaselisting.getValue());
我得到了Promise {$$ state等我附加了屏幕截图。我需要的是上图变量AccountNumber。
我正在尝试在第一个api,PaymentAmount:“100”而不是硬编码,我想从其他工厂获得它。我可以知道怎么做吗?任何帮助,将不胜感激。谢谢。
答案 0 :(得分:5)
将工厂注入第二工厂,其余工作与您在控制器中的操作相同,
myapp.factory('leaselisting', ['$http', '$cookieStore', 'cfg', 'ScrollFunction','$q','sadadpaymentapi', function ($http, $cookieStore, cfg, ScrollFunction,$q,sadadpaymentapi) {
答案 1 :(得分:2)
leaselisting
工厂有调用asyc调用并解析它的方法getValue
。
所以你可以写:
// ...
var request = {
url: urlapi,
method: 'POST',
data: {
SRActivityID: LoginID,
PaymentType: PaymentType,
PaymentAmount: leaselisting.getValue();
},
// ...
这是一个 Demo in Fiddle ,模拟你的情况。由于第二个异步调用是基于第一个,我会使用Promise链。在我的情况下,我使用$timeout
模拟异步调用,返回地址的某些部分(在您的情况下为PaymentAmount
),使用promise解析并调用其他Promise $http
app.controller('fessCntrl', function ($scope, sadadpaymentapi) {
$scope.alertSwap = function () {
$scope.data = sadadpaymentapi.getData();
};
});
app.$inject = ['$scope', 'leaselisting'];
app.factory('leaselisting', ['$timeout','$q', function($timeout, $q) {
var leaselistingobject = {
getValue: function () {
var deferred = $q.defer();
$timeout(function(){
return {address: 'Singapore, SG, Singapore, 153 Bukit Batok Street 1'};
}, 3000).then(function(val){
deferred.resolve(val);
});
return deferred.promise;
}
};
return leaselistingobject;
}]);
app.factory('sadadpaymentapi', ['$http','$q','leaselisting', function($http, $q,leaselisting) {
var leaselistingobject = {
getData: function () {
var URL = 'http://maps.googleapis.com/maps/api/geocode/json?address=';
return leaselisting.getValue().then(function(result){
URL = URL + result.address + '&sensor=true';
return $http({method: 'GET', url: URL})
}).then(function (response) {
return response.data;
});
}
};
return leaselistingobject;
}]);
答案 2 :(得分:0)
您好,您可以在控制器中执行以下操作:
leaselisting.getValue().then(function(response){
//you get account number here and pass it in another factory
var accountNumber = response.data.AccountNumber;
sadadpaymentapi.callsadad (accountNumber ).then(function(response){
//Do with your final response
});
});
和工厂代码:
myapp.factory('leaselisting', ['$http', '$cookieStore', 'cfg', 'ScrollFunction','$q', function ($http, $cookieStore, cfg, ScrollFunction,$q) {
var leaselistingobject = {};
var baseurl = cfg.Baseurl;
var LoginID = $cookieStore.get("LoginID");
var cookiePreferredLanguage = $cookieStore.get('PreferredLanguage');
leaselistingobject.getValue = function () {
var requestObj = {
url: baseurl + "api/ServiceRequest/GetROLSPSRLeaseList/",
method:'POST',
data: {
LoginID: LoginID,
AccountNumber: accountNumber
},
headers: ScrollFunction.getheaders()
};
return $http(requestObj)
}
return leaselistingobject;
}]);
确保您已将两个工厂注入控制器