如何在Angularjs打电话给Nest工厂?

时间:2017-08-17 09:33:10

标签: javascript angularjs

您好我正在开发angularjs中的Web应用程序。我有以下要求。我有一家工厂。我在下面添加了代码段。

myapp.factory('sadadpaymentapi', ['$http', '$cookieStore', 'cfg', 'ScrollFunction', 'leaselisting', function ($http, $cookieStore, cfg, ScrollFunction, leaselisting) {
    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) {
        leaselisting.leaselisting().then(function (response) {
        //Problem in calling
        }, function (error) { });
        var request = {
            url: urlapi,
            method: 'POST',
            data: {
                SRActivityID: LoginID,
                PaymentType: PaymentType,
                PaymentAmount: "100"
            },
            headers: ScrollFunction.getheaders()
        };
        return $http(request);
    }
    return sadadpaymentapiobject;
}]);

这是我的第二个工厂租赁名单

myapp.factory('leaselisting', ['$http', '$cookieStore', 'cfg', 'ScrollFunction', function ($http, $cookieStore, cfg, ScrollFunction) {
    var leaselistingobject = {};
    var baseurl = cfg.Baseurl;
    var LoginID = $cookieStore.get("LoginID");
    var cookiePreferredLanguage = $cookieStore.get('PreferredLanguage');
    leaselistingobject.leaselisting=function(){
    var requestObj = {
        url: "api/ServiceRequest/GetROLSPSRLeaseList/",
        data: {
            LoginID: LoginID,
            RSAccountNumber: $cookieStore.get("AccountNumber")
        },
        headers: ScrollFunction.getheaders()
    };
    $http(requestObj).then(function (response) {
    }, function (error) {
    });
    }
    return leaselistingobject;
}]);

我在下面的行找到了错误

 leaselisting.leaselisting().then(function (response) { //Problem in calling
        }, function (error) { });

我可能在上面的代码中做错了吗?我可以知道是否可以从另一家工厂打电话?我从租借列表中获得的响应我希望在sadadpaymentapi的callsadad函数中传递它。那么有人可以在上面的代码中解雇我吗?我收到错误无法在leaselisting.leaselisting().then(function (response) {},function(error){});

中读取未定义的属性'then'

还有什么方法可以直接注入工厂支付金额:注入这样的工厂吗?

1 个答案:

答案 0 :(得分:0)

我假设asset是一个异步函数。 首先要获得你的价值:

leaselistingobject.getValue

然后使用它。为了让所有异步操作完成,我们使用angulars leaselistingobject.getValue = function(){ var requestObj = { url: "api/ServiceRequest/getValue/" }; return $http(requestObj).then(function (response) { return response.data; }); } Here你可以找到一个小教程。

$q

要使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 leastListingPromise = leaselisting.leaselisting(); var getValuePromise = leaselisting.getValue(); $q.all([leastListingPromise, getValuePromise]).then(function (responses) { //Here you have both responses in an array var request = { url: urlapi, method: 'POST', data: { SRActivityID: LoginID, PaymentType: PaymentType, PaymentAmount: responses[1] }, headers: ScrollFunction.getheaders() }; return $http(request); }); } return sadadpaymentapiobject; }]); 返回请求的响应,请从

更改函数的结尾
leaselisting()

$http(requestObj).then(function (response) {
}, function (error) {
});

如果对可能的错误不采取任何措施,您可以省略错误功能部分:

return $http(requestObj).then(function (response) {
    return response.data;
}, function (error) {
});