如何访问控制器内的工厂方法?

时间:2016-03-03 14:14:16

标签: javascript angularjs

如何从工厂内部的嵌套$ http访问控制器内部的响应。在这里我们有两个服务调用。在另一个内部。我需要在我的控制器中响应第二个服务。我能够从控制器访问工厂以及工厂内部的响应,但是当控制器成功功能出现时,它显示成功功能未定义。

工厂代码:我在这里调用嵌套的$ http服务调用

bosAppModule.factory("ServiceCalls",function($http){

    var ServiceCalls={};
    var createFilterString = function(crudObject, callback) {
        var filterString = "";
        var keyValuePairs = [];

        // iterate over the property
        for(var property in crudObject) {
            if(!(crudObject[property] instanceof Object)) {// if it is primitive type
                // check the value is not null or undefined
                if(crudObject[property] && crudObject[property] != "")
                    // added the key value string
                    keyValuePairs.push(property + "~;~" + crudObject[property]); 
            }
        }

        // add first key value pair
        if(keyValuePairs[0])
            filterString += keyValuePairs[0];

        // iterate over the key value strings
        for(var i = 1; i < keyValuePairs.length; i++) {
            filterString += "~$~" + keyValuePairs[i];
        }

        try {
            if(callback) callback(filterString);
        } catch(e) {
            console.log("Exception inside $dataTransactor->createFilterString" + e.message);
        }
    };

//  var headers = {Authorization: COOKIES.readCookie("Authorization"),requestmode:"ACK_URL"};
//  headers.isRender = file.isRender;
//  if(file.inputDataHeaders)
//      headers.inputData = file.inputDataHeaders;

    ServiceCalls.getData = function(filterObject, file){

        createFilterString(filterObject, function(filterString){
            var headers = {Authorization: COOKIES.readCookie("Authorization"),requestmode:"ACK_URL"};
            headers.isRender = file.isRender;
            if(file.inputDataHeaders)
                headers.inputData = file.inputDataHeaders;

            $http({
                method: 'GET',
                url: file.fileUrl + "/" + $securityComponent.cryptograghicFunctions.encryptor(filterString),
                headers: headers
            })
            .then(function(requestHandlerResponce) {
                console.log(requestHandlerResponce);
                $http({
                    method: 'GET',
                    url: requestHandlerResponce.data.links[1].href,
                    headers: headers
                }).then(function(responceHandlerResponce) {
                    console.log("##### : "+JSON.stringify(responceHandlerResponce.data));
                    return responceHandlerResponce;
                });
            })



        });

    };

    return ServiceCalls
});

控制器代码:这里我需要回复

bosAppModule
        .controller(
                "custom-entity-design-ctrl",
                function($scope, $document, $http, $localStorage, navigateEntityUrl, entityFormation,layoutDesignFactory, ServiceCalls) {   

                    var layoutDesignFac=new layoutDesignFactory();
                    var entityJson='{"entityInfo":{"entity":"","tenantId":"2b69af63-e2dc-43e5-9f0e-9fde52032d4c","timeStamp":"Tue Jun 16 2015 19:05:09 GMT+0530 (India Standard Time)"},"collections":{"Entity":{"meta":{"parentReference":"***","pkName":"***","fkName":"***"},"rowSet":[],"rowFilter":[]}}}';
                    var crudObject = {};
                    var file = { 
                        fileUrl: $config.UIMetaData,
                        inputDataHeaders: entityJson
                    }; 


                    ServiceCalls.getData(crudObject,file).success(function(response){console.log(response)});

});

1 个答案:

答案 0 :(得分:0)

您的服务应该将promises(在您的情况下为$ http调用)返回给控制器:

return $http({              // return this promise
  method: 'GET',
  url: file.fileUrl + "/" + $securityComponent.cryptograghicFunctions.encryptor(filterString),
    headers: headers
}).then(function(requestHandlerResponce) {
  console.log(requestHandlerResponce);
  return $http({            // return this promise as well
    method: 'GET',
    url: requestHandlerResponce.data.links[1].href,
    headers: headers
}).then(function(responceHandlerResponce) {
  console.log("##### : "+JSON.stringify(responceHandlerResponce.data));
  return responceHandlerResponce;
});

为了保持一致,请尝试在控制器中使用标准.then方法而不是.success.error

ServiceCalls.getData(crudObject,file).then(function(response) {
  console.log(response)
});

最后有点无关紧要的说明,我认为&#39;回应&#39;在您的服务中拼写错误;)