简体   繁体   中英

Getting data from a web service using AngularJs. Am I doing it in the correct way?

I am using following codes to get data from a webservice.

I want to know that it is a correct way ,Is there any other better way to do this and also how can i handle the error.?

My factory object to get dada from a services

.factory('userSubCat', function($http,$rootScope) {

return {
    getSubs: function(){
  return $http.post('http://xxxx.com/demo/app-api/subcategory.php', {"categoryId":$rootScope.catIdSelected}).then(function(response){
          return response;
        });
    }
}
})

I have included the following code to show the loading icon

$scope.loadingIndicator = $ionicLoading.show({
    content: 'Loading Data',
    animation: 'fade-in',
    showBackdrop: false,
    maxWidth: 200,
    showDelay: 500
});

I am consuming the data as mentioned below

 userSubCat.getSubs().then(function(data){

             $scope.playlists =data.data.subcategoryList;

             $scope.loadingIndicator.hide();

        });

I want to know that it is a correct way ,Is there any other better way to do this and also how can i handle the error?

Regarding your first question: Please edit your code and clarify where and how you call the function userSubCat.getSubs().

Regarding your second question: To get aware of errors when using $http use the following signature:

$http.post('/someUrl', data, config).then(successCallback, errorCallback);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM