-
How to call a service function of child controller inside a function in main controller
over 7 years ago
-
over 7 years ago
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); }); } } }; })
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! -
1 Answer(s)