AngularJs 自定义ajax服务为请求添加加载动画
2017-02-10 16:00
399 查看
一、此处示例使用iconic的动画加载
1.自定义ajax服务封装
//全局配置请求处理
app.service('$ajax', function ($http, $ionicLoading) {
this.get = function (url, data, onSuccess, onError) {
$ionicLoading.show(); //显示动画
$http.get(url, {
param: data
})
.then(function (result) {
if (result.status == 200) {
if (onSuccess)
onSuccess(result.data);
} else {
if (onError)
onError(result.data);
}
$ionicLoading.hide(); //隐藏动画
}).catch(function (err) {
console.error(err);
if (onError)
onError(err.data);
$ionicLoading.hide(); //隐藏动画
});
}
});2.页面内容
<div class="list">
<div class="item">
<button class="button button-balanced" ng-click="show($event);">加载</button>
</div>
</div>
<ion-content overflow-scroll="false">{{content}}</ion-content>
3.使用实例
var app = angular.module('myApp', ['ionic']);
//全局配置加载动作
app.constant('$ionicLoadingConfig', {
template: '<ion-spinner icon="lines" class="spinner-calm"></ion-spinner>',
animation: 'fade-in'
});
更多:
AngularJs $http 请求服务整理
AngularJs constant()和value()
AngularJS阻止事件冒泡$event.stopPropagation()
1.自定义ajax服务封装
//全局配置请求处理
app.service('$ajax', function ($http, $ionicLoading) {
this.get = function (url, data, onSuccess, onError) {
$ionicLoading.show(); //显示动画
$http.get(url, {
param: data
})
.then(function (result) {
if (result.status == 200) {
if (onSuccess)
onSuccess(result.data);
} else {
if (onError)
onError(result.data);
}
$ionicLoading.hide(); //隐藏动画
}).catch(function (err) {
console.error(err);
if (onError)
onError(err.data);
$ionicLoading.hide(); //隐藏动画
});
}
});2.页面内容
<div class="list">
<div class="item">
<button class="button button-balanced" ng-click="show($event);">加载</button>
</div>
</div>
<ion-content overflow-scroll="false">{{content}}</ion-content>
3.使用实例
var app = angular.module('myApp', ['ionic']);
//全局配置加载动作
app.constant('$ionicLoadingConfig', {
template: '<ion-spinner icon="lines" class="spinner-calm"></ion-spinner>',
animation: 'fade-in'
});
app.controller('myCtrl', function ($scope, $http, $ajax) { $scope.show = function ($event) { //显示加载 $ajax.get('http://www.gongjuji.net', {}, function (data) { $scope.content = data; }); $event.stopPropagation(); } });显示效果:
更多:
AngularJs $http 请求服务整理
AngularJs constant()和value()
AngularJS阻止事件冒泡$event.stopPropagation()
相关文章推荐
- Android为网络请求自定义加载动画
- 自定义网络请求时加载动画dialog
- 自定义dialog,加载数据或网络请求时 dialog+动画(两种样式分享)
- jQuery的ajax请求添加"加载中..."提示
- js对js加载拼接的html代码中元素添加ajax方法请求后台数据
- ajax 请求 添加遮罩和等待动画
- ajax请求添加自定义header参数
- 用Ajax加载Google地图并向地图上添加标注信息
- 为AJAX 请求添加客户端事件
- 在SQL Server 2005 Express 中添加报表服务后 (Business Intelligence Development Studio) vs2005 加载程序集问题解决办法
- ajax请求添加客户端事件的激发顺序和JSON
- ASP.NET:为 AJAX 请求添加客户端事件
- ASP.NET AJAX自定义应用程序服务的服务器端实现
- 在ASP.NET AJAX中使用应用程序服务和本地化(5):自定义应用程序服务的服务器端实现
- 添加AjaxControlToolkit.dll时出现未能加载文件或程序集
- 自定义Loading载入状态(含图标) 及解决Flex中GIF动画图片加载时显示动起来
- 为CSS Visual Filters and Transitions 的动画添加自定义加速度
- 自定义Alert Confirm对话框[2007-09-07添加显示Progress](需Asp.net Ajax支持)
- 添加/删除请求开始事件的方法 (asp.net ajax框架)
- ajax 发布web 服务 未能加载类型的解决办法