即使 ng-show 评估为 false,我们的加载图标(微调器)有时仍然可见。
这是指令:
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
但 NO 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;
}
我正在使用 angular 1.2.3 和 angular-animate 1.2.3