2014-01-09 20 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點 –

相關問題