0

I want to make a directive with shows a spinner when $http request is executing.

I thought this would be working (e.g. load users):

Main Controller

app.controller('UserListController', function($scope, $http) {

    $scope.users = [];
    $scope.loading = true;

    $http.get('/users').then(function(response) {
        $scope.users = response.data;
    }).finally(function() { $scope.loading = false; });

});

Template using the directive

<div request-loading="loading">
    <ul>
       <li ng-repeat="user in users">{{ user.name }}</li>
    </ul>
</div>

request-loading-directive.js

app.directive('requestLoading', function() {
    return {
        scope: {
           requestLoading: '='
        },

        transclude: true, // I think this is not Ok

        templateUrl: 'request-loading-directive.html'
    };
});

request-loading-template.html

<div ng-show="requestLoading" class="super-cool-spinner">
    Loading text with the spinner...
</div>

// This is what is not working
// I thought the content of the users (ul and li) would be render in this div
// when the request is Ok.
<div ng-transclude ng-show="! requestLoading"></div>

Any help would be nice! Thx.

Ulrira
  • 229
  • 4
  • 12
  • have you tried to put `$scope.loading = false;` at the end of the `.then()` block ? – Freezystem Oct 17 '15 at 06:47
  • Working fine here: http://plnkr.co/edit/seWdgwXi9KOLEY2hclPe?p=preview. I replaced the http promise by a timeout promise to test it, but it shouldn't change anything. Please create a plunkr reproducing the problem. Check your console for errors. – JB Nizet Oct 17 '15 at 07:21
  • I don't understand what is not working correctly ? here is a fork of your [plunker](http://plnkr.co/edit/nzU1Nj9TlfAZy8niT6mF?p=preview) with an $http call. The loader seems to work correctly. – Freezystem Oct 17 '15 at 08:43
  • Well, magic: it is working now. I check and I forgot "ng-show" of the ng-transclude. Sorry about that! – Ulrira Oct 17 '15 at 09:53

0 Answers0