2014-01-09 24 views
3

即使ng-show计算结果为false,我们的加载图标(spinner)仍然可见。ng-show指令的计算结果为false后,为什么不添加ng-hide类?

这里的指令:

angular.module("app.directives").directive("loadingIcon", function() { 
return { 
    restrict: "A", 
    replace: true, 
    link: function (scope, element) { 

     scope.showLoader = false; 

     scope.$on('event:httpRequestStarted', function() { 
     // got the request start notification, show the element 
     scope.showLoader = true; 
     }); 

     scope.$on('event:httpRequestsCompleted', function() { 
     // got the request completed notification, hide the element 
     scope.showLoader = false; 

     }); 
    }, 
    template: "<div ng-show='showLoader' class='fade-in-out loading-icon spin-fx ng-hide' ng-cloak></div>" 
    } 
}); 

这里是广播,如果微调应该是开还是不httpInterceptor:

// interceptor to show loading icon 
$httpProvider.interceptors.push(['$q','$rootScope', function($q, $rootScope) { 
    window.requestCount = 0; 

    return { 
    request: function(config) { 

     window.requestCount += 1; 
     console.log('start request', config, window.requestCount) 
     $rootScope.$broadcast('event:httpRequestStarted'); 
     return config || $q.when(config); 
    }, 

    response: function(response) { 

     $rootScope.$broadcast('event:httpRequestSuccess', response); 
     window.requestCount -= 1; 
     console.log('got response', response, window.requestCount) 
     if ((window.requestCount) === 0) { 
     console.log('stop spinny') 
     $rootScope.$broadcast('event:httpRequestsCompleted'); 
     } 
     return response || $q.when(response); 
    }, 

从输出的console.log和window.requestCount ,逻辑是正确的。大部分情况下,这是有效的。但有时(可能是竞赛状态?),加载图标仍然存在于类ng-hide-add ng-animate-active ng-hide-add-active,但是没有ng-hide。我认为如果ng-show错误,它应该添加一个ng-hide类?

任何人都可以了解竞争条件可能是什么?

编辑:

是,ngAnimate模块包含在我们的应用程序。下面是加载图标的CSS:

.loading-icon { 
    background-image: url("/images/loading-icon.png"); 
    background-repeat: no-repeat; 
    background-position: 0 0; 
    width: 40px; 
    height: 40px; 
    z-index: 100000; 
    position: fixed; 
    right: 50%; 
    top: 50%; 
    margin: -20px 0 0 -20px; 
} 

.spin-fx { 
    -moz-animation: spin-fx 2s infinite linear; 
    -o-animation: spin-fx 2s infinite linear; 
    -webkit-animation: spin-fx 2s infinite linear; 
    animation: spin-fx 2s infinite linear; 
} 

// loading icon 
.fade-in-out { 
    transition:linear 1s; 
} 

.fade-in-out.ng-enter { 
    opacity:0; 
} 

.fade-in-out.ng-enter-active { 
    opacity:1; 
} 

.fade-in-out.ng-leave { 
    opacity:1; 
} 

.fade-in-out.ng-leave-active { 
    opacity:0; 
} 

我使用的角度1.2.3和角动画1.2.3

+0

'ng-show'等于false不会添加'ng-hide'。它添加了一个'style = display:none'。无论如何,它应该起作用。 – Beterraba

+0

@Beterraba不,ng-hide'类是在'ng-show'的表达是falsey时添加的 –

+0

您需要在您的应用中使用ngAnimate模块吗?你的微调使用css让它旋转? – Daiwei

回答

2
scope.$on('event:httpRequestStarted', function() { 
    scope.showLoader = true; 
    scope.$apply(); // Apply changes 
}); 

scope.$on('event:httpRequestsCompleted', function() { 
    scope.showLoader = false; 
    scope.$apply(); // Apply changes 
}); 

AngularJS使用事件(如$on时不会更改支票,$broadcast$emit)。 $scope.apply()将手动检查更改。

+0

我在使用范围之前试过,$ apply(function(){scope.showLoader = false;})但是我得到一个错误,就是$ digest循环已经在进行中。 – Homan

+0

如果在'$ rootScope。$ apply()'中包装'$ rootScope。$ broadcast'调用会怎么样?它可能会将'$ apply()'移到调用栈中足够高的位置。 [AngularJS反模式](https://github.com/angular/angular.js/wiki/Anti-Patterns)第2点 –

相关问题