简体   繁体   English

如何实现加载指令AngularJS

[英]How to implement loading directive AngularJS

I am trying to implement loading sniper... But problem is when I put sniper inside html it not working. 我正在尝试实现加载狙击手...但是问题是当我将狙击手放在html内时它不起作用。 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: 这是我的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: 在我的commentsController中,我这样添加:

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

Anyone know what is problem? 有人知道是什么问题吗?

Check working demo: JSFiddle . 检查工作演示: JSFiddle Your problem is maybe just because angular.element does not have functions show and hide . 您的问题可能仅仅是因为angular.element没有showhide功能。

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: 和HTML:

<div ng-show="iAmLoading" loading>Loading ({{ remained }} remainded) ...</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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