0

Since I am using ajax request using $http. It takes a long time since my operation on server takes time. I need to show loader while processing request, but the loader does not show. Although my code seems correct. I tried different methods but did not work.

Index.html

<body ng-app="app">

    <!-- loader, can be used on multiple pages-->
    <div class="loading loader-quart" ng-show="isLoading"></div>

<!--         my logic       --> 

</body>

addCtrl.js

//method to get all the attributes and send to server using service
    $scope.add = function () {
        if ($scope.Option == 'newInstance')
            $scope.singleObject.FK_Name = 'MetisEmptyTemplate';
        $rootScope.isLoading = true;
        var featuresList = websiteService.getUpdatedTree($scope.treeDataSource);
        var formData = new Website("", $scope.singleObject.Name, $scope.singleObject.DisplayName, $scope.singleObject.Description, $scope.singleObject.State, "", $scope.singleObject.FK_Name, $scope.singleObject.Email, featuresList);

        websiteService.addwebsite(formData);
        $rootScope.isLoading = false;
    }

websiteService.js

//service to add website
    this.addwebsite = function (website) {
        $http({
            method: 'POST',
            url: $rootScope.url + 'Add',
            data: JSON.stringify(website),
            contentType: 'application/json'
        }).success(function (data) {
            alert(data);
        }).error(function (data, status, headers, config) {
            //alert(data);
        });
    }

Since I am going to turn isLoading as "true" in start and then after request completes I turn isLoading "false". Where is the problem in code?

2
  • You set it on the $rootScope, so you need to do: ng-show="$root.isLoading" Commented Oct 13, 2016 at 11:36
  • But that also does not work. Since rootscope can access everything within app Commented Oct 13, 2016 at 11:38

3 Answers 3

1

Your websiteServices code gets executed asynchronously. Which means that the above code would display the loader and then pretty much hide it again instantly.

To handle async code in the controller you must return a promise from the service and put the hiding of the spinner in a callback function using .then().

service:

this.addwebsite = function (website) {
    var deferred = $q.defer();
    $http({
        method: 'POST',
        url: $rootScope.url + 'Add',
        data: JSON.stringify(website),
        contentType: 'application/json'
    }).success(function (data) {
        alert(data);
        deferred.resolve(data);
    }).error(function (data, status, headers, config) {
        //alert(data);
        deferred.reject(data);
    });
    return deferred.promise
}

controller:

websiteService.addwebsite(formData).then(function(){
    $rootScope.isLoading = false
});
Sign up to request clarification or add additional context in comments.

6 Comments

do i need to include dependency of $q ?
Yes in your service you must inject $q. Its built in to angular so just inject it
Im not sure I understand the question, for more information about angulars promise implementation see: docs.angularjs.org/api/ng/service/$q
Hey loader is working but i ran to a problem Cannot read property 'then' of undefined at websiteService.addwebsite(formData)
Seems like you're not returning the promise correctly from the service then.
|
0
    this.insertMliveResponse = function(data){
        var defer=$q.defer();
        var requestURL='/mlive-portlet/rest/mliveResponseService/insertmLiveResponse';
        httpRequest(requestURL,data).then(function(data){
                defer.resolve(data.data);
        },function(data){
            defer.reject(data.data);
        })
        return defer.promise;
    }

2 Comments

try to use defer.promise
why do i need to return defer.Promise?
0

If you are making request then, I think the best way to show hide loader is interceptor

In my snippet, I am using loader service to activate/deactivate loader

For Eg:

// http.config.js  file
export function httpConfig($httpProvider, AuthInterceptProvider) {
  'ngInject';
  AuthInterceptProvider.interceptAuth(true);

  // added for show loader
  $httpProvider.interceptors.push(function (loaderService, $q) {
    'ngInject';
    return {
      'request': function (config) {
        loaderService.switchLoaderModeOn();
        return config;
      },

      'requestError': function (rejection) {
        loaderService.switchLoaderModeOff();
        return $q.reject(rejection);
      },

      'response': function (response) {
        loaderService.switchLoaderModeOff();
        return response;
      },

      'responseError': function (rejection) {
        loaderService.switchLoaderModeOff();
        return $q.reject(rejection);
      }
    };
  });
}

// and in your module.js file
import {httpConfig} from './config/http.config';

.config(httpConfig)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.