微信数据请求时显示加载中

This commit is contained in:
txz 2016-05-03 15:37:35 +08:00
parent 151661830f
commit de8b303b93
4 changed files with 47 additions and 23 deletions

View File

@ -1,6 +1,7 @@
<div id="container">
<!-- 模板1开始可以使用scripttype设置为text/html来存放模板片段并且用id标示 -->
<div loading-spinner></div>
<div ng-repeat="act in activities">
<div ng-if="act.container_type=='Course'">
<div ng-if="act.act_type=='HomeworkCommon'">

View File

@ -14,11 +14,6 @@
</head>
<body>
<div ng-view>
<div class="loading-modal loading-bg" ng-if="loading">
<div class="loading-box">
<img src="/images/wechat/loading.gif" alt=""/><span>加载中...</span>
</div>
</div>
</div>

View File

@ -3,6 +3,7 @@ var apiUrl = '/api/v1/';
var debug = false; //调试标志,如果在本地请置为true
if(debug===true){
//apiUrl = 'http://localhost:3000/api/v1/';
apiUrl = 'https://www.trustie.net/api/v1/';
}
@ -399,6 +400,25 @@ app.directive('textAutoHeight', function($timeout){
}
});
app.directive('loadingSpinner', function ($http) {
return {
restrict: 'A',
replace: true,
template: '<div class="loading-bg"><div class="loading-box"><img src="/images/wechat/loading.gif" alt=""/><span>加载中...</span></div></div>',
link: function (scope, element, attrs) {
scope.$watch('activeCalls', function (newVal, oldVal) {
if (newVal == 0) {
$(element).hide();
}
else {
$(element).show();
}
});
}
};
});
app.config(['$routeProvider',"$httpProvider",function ($routeProvider, $httpProvider) {
$routeProvider
.when('/activities', {
@ -436,22 +456,30 @@ app.config(['$routeProvider',"$httpProvider",function ($routeProvider, $httpProv
.otherwise({
redirectTo: '/activities'
});
//$httpProvider.interceptors.push('timestampMarker');
}]);
//loading
app.factory('timestampMarker', ["$rootScope", function ($rootScope) {
var timestampMarker = {
request: function (config) {
$rootScope.loading = true;
config.requestTimestamp = new Date().getTime();
return config;
},
response: function (response) {
// $rootScope.loading = false;
response.config.responseTimestamp = new Date().getTime();
return response;
}
};
return timestampMarker;
//监听异步请求
$httpProvider.interceptors.push(function ($q, $rootScope) {
if ($rootScope.activeCalls == undefined) {
$rootScope.activeCalls = 0;
}
return {
request: function (config) {
$rootScope.activeCalls += 1;
return config;
},
requestError: function (rejection) {
$rootScope.activeCalls -= 1;
return rejection;
},
response: function (response) {
$rootScope.activeCalls -= 1;
return response;
},
responseError: function (rejection) {
$rootScope.activeCalls -= 1;
return rejection;
}
};
});
}]);

View File

@ -76,4 +76,4 @@ a.underline {text-decoration:underline;}
.loading-bg {position:fixed; width:100%; height:100%; left:0; top:0; z-index:99; background:rgba(206, 206, 206, 0.3); overflow:hidden;}
.loading-box {position:absolute; top:50%; background:white; width:160px; height:72px; left:50%; margin-top:-36px; margin-left:-80px; text-align:center;}
.loading-box img {margin-top: 3px; text-align: center;}
.loading-box span {display: block;}
.loading-box span {display: block; font-size:12px;}