TEL:400-8793-956
当前位置:程序、服务器

如何在主控制器的功能内调用子控制器的服务功能

提问者: 近期获赞: 浏览人数: 发布时间:2021-01-28 13:32:29

 问:我在客户端控制器和服务中有一个函数“ getClient()”,该函数返回客户端信息。现在,我想恢复该功能。

 
我要达到的目的是,当客户单击配置文件按钮时,他可以获取客户信息并查看其配置文件,而我正在使用现有的 getclient函数, 但是问题是我无法设置该值。在Profile Controller的此处 ClientService.setClient(aClient);
 
当我单击配置文件按钮时,发生了什么情况,控件进入了配置文件功能,并且在这一行上 ClientService.getClient(aClient).then(function()  {**
 
它移动到服务的getClient函数,并且在从此处通过客户端服务调用API之前无法找到OrganizationId值 if(theClient.OrganizationId){
// API调用
 
他们在逻辑上有什么错吗?需要帮助来解决此问题。提前致谢!
 
这是我的客户服务代码:
 
App.factory('ClientService',function($ http,$ cookieStore,uuid2,API_URL,REQUEST_HEADER,RESPONSE_CODE){
 
    / ***************************************************** **************
        变量
    *************************************** ********************************* /
    var theClient = $ cookieStore.get('UserData')|| {};
    
 
    / ***************************************************** **************
        方法
    *************************************** ********************************* /
    return {
 
        getClient:function(){
              
            if(theClient.OrganizationId){
 
                // API调用
                var promise = $ http.get(API_URL.GET_CLIENT + theClient.OrganizationId +“&CorrelationId =” + uuid2.newuuid()+“&ContextOrganizationId = 009”,REQUEST_HEADER)
                .then (function(aGetClientResponse){//成功回调
                       
                    return [aGetClientResponse.data.GetClientResponse.Result.ResponseCode,aGetClientResponse.data.GetClientResponse];
                },
                function(aGetClientResponse){//错误回调
 
                    返回[aGetClientResponse.status,''];
                });
            }
 
            回报承诺;
        },
 
        setClient:function(aClient){
 
            theClient =一个客户;
            $ cookieStore.put('UserData',theClient);
        }
    }
});
 
 
答:我确实经过了您的查询,但是我不清楚您到底想做什么。但是据我了解,因为if(theClient.OrganizationId)条件不满足,所以未调用您的getClient函数。
因此,根据我的理解,这是您需要具有的新控制器和服务。
App.factory('ClientService', function($http, $cookieStore, uuid2, API_URL, REQUEST_HEADER, RESPONSE_CODE) {
    return {
        getClient: function(getOrganizationId) {
                //API Call
                var promise = $http.get(API_URL.GET_CLIENT+getOrganizationId+"&CorrelationId="+uuid2.newuuid()+"&ContextOrganizationId=009", REQUEST_HEADER)
                return promise;
            }
        }
});
App.controller("globalctrl",function($scope,$rootscope,$window,$location,$cookieStore,ClientService,AuthenticationService,toastr)){
    $scope.profile = function(theClient){
        var getCurrentClient = theClient;
        var getOrganizationId = getCurrentClient.OrganizationId;
        if ($rootscope.globalSession.UserRole == 'Admin') {
            if (getOrganizationId) {
                ClientService.getClient(getOrganizationId).success(function(getClientResponse){
                    if (getclientResponse[0] == $scope.RESPONSE_CODE.CM_SUCCESS) {
                        $scope.myprofile = getclientResponse[1];
                        $location.path("/profile");
                    }else if (getclientResponse[0] == $scope.RESPONSE_CODE.GENERAL_WARNING ) {
                        toastr.warning($scope.USER_MESSAGE.GET_USER_WARNING, '');
                    }else{
                        toastr.warning($scope.USER_MESSAGE.SERVICE_NOT_AVAILABLE, '');
                    }
                }).error(function(getClientError)){
                    console.log(getClientError);
                });
            }
        } 
    };
})
1.仅使用Service(在此特别使用)来调用您的API。
2.将服务(ClientService)注入控制器。
3.将条件逻辑放在控制器中。由于您在此处获得“ theClient”值,因此请在此处使用组织ID进行检查。因为仅当您具有组织ID时,您才想调用API!因此,在此处检查它,然后通过将此组织ID传递给服务来调用服务getClient函数。
4.在您的服务中,您需要提取该ID以用于发出http get请求。
5.无论API响应是什么,您都会在getClientResponse(来自API的响应数据)中接收它,然后相应地使用它。
 
尝试使用此逻辑和代码,让我知道您的查询是否已解决,并且也告诉我您的要求是否与我从您的描述和代码中提取的内容有所不同。
上一篇: Fb广告活动与点击链接统计数据之间存在极大差异
下一篇: 学习内容写作的简便方法