1

I am trying to implement loading sniper... But problem is when I put sniper inside html it not working. here is my direcitve:

angular.module('commentsApp', [])
        .directive('loading', loading);


function loading($http) {
    return {
        restrict: 'A',
        link: function (scope, elm, attrs)
        {
            scope.isLoading = function () {
                return $http.pendingRequests.length > 0;
            };

            scope.$watch(scope.isLoading, function (v)
            {
                if (v) {
                    elm.show();
                } else {
                    elm.hide();
                }
            });
        }
    };
}

Here is my html:

<div class="loading-spiner-holder" data-loading ><div class="loading-spiner"><img src="http://www.nasa.gov/multimedia/videogallery/ajax-loader.gif" /></div></div>

In my commentsController I added like this:

angular.module('commentsApp')
        .controller('CommentsCtrl',function(loading){

Anyone know what is problem?

5
  • Could please add a Plunkr? Ideally with a button "create xhr request". Commented Sep 17, 2015 at 10:48
  • The first issue I see is that your directive is called loading, but in html you call it data-loading. Commented Sep 17, 2015 at 10:51
  • I also tried with just loading but don't work. Commented Sep 17, 2015 at 10:52
  • what do u need the loader for? Is it gonna be dynamically used for promises? or just render while loading images? Commented Sep 17, 2015 at 10:59
  • I need to wait till finish all $http GET requests... Commented Sep 17, 2015 at 11:04

1 Answer 1

2

Check working demo: JSFiddle. Your problem is maybe just because angular.element does not have functions show and hide.

scope.isLoading = function () {
    scope.remained = $http.pendingRequests.length;
    return $http.pendingRequests.length > 0;
};
scope.iAmLoading = scope.isLoading();

scope.$watch(scope.isLoading, function (v) {
    scope.iAmLoading = v;
    if (!v) console.log('All loaded');
});

And HTML:

<div ng-show="iAmLoading" loading>Loading ({{ remained }} remainded) ...</div>
Sign up to request clarification or add additional context in comments.

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.