在加载数据之前显示Ionic加载
Show Ionic loading till data loaded
我正在使用Ionic框架开发一个应用程序,我想在其中显示加载转轮,直到从API 完全加载数据
Controller.js
.controller('PrayersCtrl', function($scope,Prayers,$ionicLoading,$timeout) {
$ionicLoading.show({
content: 'Loading',
animation: 'fade-in',
showBackdrop: true,
maxWidth: 200,
showDelay: 0
});
$scope.prayers = Prayers.query();
$ionicLoading.hide();
})
和Services.js
angular.module('starter.services', [])
.factory('Prayers', function($resource) {
return $resource(base_url+'/wp/v2/posts');
});
在这种情况下,Spinning wheel甚至不显示(我的意思是它太快了,以至于它在毫秒内显示和消失)。加载数据所需的时间,同时在加载数据之前显示空白页。我想显示转轮,直到数据加载到页面中。
已编辑
我还在controller.js 中尝试了超时
.controller('PrayersCtrl', function($scope, $stateParams,Prayers,$ionicLoading,$timeout) {
$ionicLoading.show({
content: 'Loading',
animation: 'fade-in',
showBackdrop: true,
maxWidth: 200,
showDelay: 0
});
$timeout(function () {
$ionicLoading.hide();
$scope.prayers = Prayers.query();
}, 2000);
})
但在这种情况下,转轮在2000毫秒后消失,如代码所示;我想旋转代码直到数据完成加载。
在controller.js中,我在查询后添加了$promise,并对网络错误的显示消息进行了一些更改
.controller('PrayersCtrl', function($scope,Prayers,$ionicLoading,$timeout) {
$ionicLoading.show({
content: 'Loading',
animation: 'fade-in',
showBackdrop: true,
maxWidth: 200,
showDelay: 0
});
$scope.prayers = Prayers.query().$promise.then(function(result){
console.log(result);
$scope.prayers=result;
$ionicLoading.hide();
}, function(error){
console.log(error);
$ionicLoading.hide();
$ionicLoading.show({
template: 'Network Error',
scope: $scope
});
$timeout(function() {
$ionicLoading.hide();
}, 2000);
})
})
并使服务恢复到其原始形式
angular.module('starter.services', [])
.factory('Prayers', function($resource) {
return $resource(base_url+'/wp/v2/posts');
});
Muhammad,我不得不在推特上发布你的答案,让它为我工作。这是我的控制器。
.controller('PrayersCtrl', function($scope, $ionicLoading, Prayers, $timeout) {
$ionicLoading.show({
template: '<ion-spinner icon="spiral" class="spinner-positive"></ion-spinner> <br>Loading...',
noBackdrop: true,
animation: 'fade-in'
});
Prayers.query().$promise.then(function(result){
$scope.prayers = result;
$ionicLoading.hide();
}, function(error) {
// error handling here
$ionicLoading.hide();
$ionicLoading.show({
template: "unable to connect",
noBackdrop: true
});
$timeout(function() {
$ionicLoading.hide();
}, 2000);
})
})
尝试此解决方案
.controller('PrayersCtrl', function($scope,Prayers,$ionicLoading,$timeout) {
$ionicLoading.show({
content: 'Loading',
animation: 'fade-in',
showBackdrop: true,
maxWidth: 200,
showDelay: 0
});
$scope.prayers = Prayers.query().then(function(result){
console.log(result);
$ionicLoading.hide();
}, function(error){
console.log(error);
$ionicLoading.hide();
})
})
在您的工厂中,将代码编辑为
angular.module('starter.services', [])
.factory('Prayers', function($resource, $q) {
var self = this;
self.query = function(){
var defer = $q.defer();
$resource(base_url+'/wp/v2/posts').then(function(result){
console.log(result);
defer.resolve("success");
}, function(error){
console.log(error);
defer.reject("error");
})
return defer.promise;
}
return self;
});
相关文章:
- 如何在生成下载文件时显示加载动画
- 如何在谷歌字体加载时显示加载图像
- 有没有比在app.js上绑定模块名称更好的方法来动态加载视图模型和视图以显示模态
- 如何用ajax实现加载显示
- 使用javascript加载/显示图像并获得%下载
- 谷歌地图不会加载/显示KML文件
- 我的图片不是使用惰性加载显示的
- javascript d3可视化不加载/显示,直到点击HTML输入
- 导航标签动态加载/显示
- jQuery Select2远程数据加载:显示选项而不是占位符
- Console.log一些东西一旦异步Facebook喜欢按钮加载(显示)
- PHP停止表单重新加载显示错误Js
- 预加载显示/隐藏分区
- ReCaptcha不会在IE中加载/显示,但在Chrome和safari中可以完美运行
- dom事件——通过JavaScript加载显示一个又一个元素
- jQuery延迟加载-显示的问题:无
- Json.数据没有正确加载/显示
- 在每个页面加载显示不同的图像
- 如何检测PDF数据对象是否在html对象中加载/显示/有效
- Jquery添加页面加载显示请求的标签功能