Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to call a service function of child controller inside a function in main controller

    • 0
    • 0
    • 0
    • 1
    • 0
    • 1
    • 0
    • 616
    Answer it

    I have a a function in client controller and service that is "getClient()" which return a client information. Now I want to resue the function.

    What I am trying to achieve is that when client click on profile button he can getclient information and view his profile and I am using my existing getclient Function but the problem is I am unable to set the value. In Profile Controller over here ClientService.setClient(aClient);

    What is happening the it is when I click on profile button, control comes in profile function and on this line ClientService.getClient(aClient).then(function() {**

    It moves to getClient function of the service and unable to find OrganizationId value before calling API from client service over here if(theClient.OrganizationId) {
    //API Call

    Is their anything wrong in logically? Need help to resolve this Issue. Thanks in Advance!

    This is my Client Service Code:

    App.factory('ClientService', function($http, $cookieStore, uuid2, API_URL, REQUEST_HEADER, RESPONSE_CODE) {

        /***************************************************************
            VARIABLES
        ****************************************************************/
        var theClient = $cookieStore.get('UserData') || {};
        

        /***************************************************************
            METHODS
        ****************************************************************/
        return {
     
            getClient: function() {
                  
                if(theClient.OrganizationId) {

                    //API Call
                    var promise = $http.get(API_URL.GET_CLIENT+theClient.OrganizationId+"&CorrelationId="+uuid2.newuuid()+"&ContextOrganizationId=009", REQUEST_HEADER).then(
                    function(aGetClientResponse) { //Success Callback
                           
                        return [aGetClientResponse.data.GetClientResponse.Result.ResponseCode, aGetClientResponse.data.GetClientResponse];
                    },
                    function(aGetClientResponse) { //Error Callback

                        return [aGetClientResponse.status,''];
                    });
                }

                return promise;
            },

            setClient: function(aClient) {

                theClient = aClient;
                $cookieStore.put('UserData', theClient);
            }
        }
    });

    This is my Main Controller's Code

    How to call a service function of child controller inside a function in main controller

 1 Answer(s)

  • Hi,
    I did go through your query but I am little unclear of what exactly you want to do. But what I understood is that your getClient function is not being called because the if(theClient.OrganizationId) condition do not get satisfied.
    So, Here is the new controller and Service that you need to have as per my understanding.
    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.Use Service(specifically here) only to make call to your API.
    2.Inject your service (ClientService) into your controller.
    3.Put your condition logic here in the controller. Since you are getting your 'theClient' value here,use the organization id check over here. Beacuse you want to make call to your API only when you have organization id,right!! So,check it here and then call the service getClient function by passing this organization id to it.
    4.In your service ,you'll need to extract that id to use it for making the http get request.
    5.Whatever will be the API response you'll receive it in the getClientResponse(response data from the API) and then use it accordingly.

    Try using this logic and code and let me know if your query is resolved and also do tell me if your requirement was something else from what I extracted from your description and code.
    If you find my answer useful,please do mark it correct.

    Thankyou!
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: