[英]Ionic HTTP Request Delayed Update
I've got a basic ionic application which lists jobs on a page, when a job is clicked this calls my REST API and returns people on that job. 我有一个基本的离子应用程序,该应用程序在页面上列出了作业,当单击某个作业时,这将调用我的REST API并返回该作业上的人员。
The problem I have is that when the job is clicked the page loads without the data initially then if I pull to refresh the page shows the results? 我的问题是,当单击作业时,页面加载时最初没有数据,那么如果我拉刷新页面会显示结果?
As the page is shown the results of the people should be there already not after I pull to refresh? 如图所示,在刷新后人们的结果应该已经不存在了吗?
Does anyone know why my results don't show on page load? 有谁知道为什么我的结果不显示在页面加载中?
My job list 我的工作清单
Job 1 <a href="#/job/1">Begin</a>
Job 2 <a href="#/job/2">Begin</a>
Job 3 <a href="#/job/3">Begin</a>
When a job is clicked this loads my state & controller 单击作业后,这会加载我的状态和控制器
State 州
.state('job', {
url: '/job/:jobID',
templateUrl: 'templates/job.html',
controller: 'jobCtrl'
})
Controller 控制者
.controller('jobCtrl', function($scope, $stateParams, $http, $state) {
$scope.jobId = $stateParams.jobID;
$scope.data = {};
$http.get('https://domain/api/job?token=' + localStorage.getItem("token") + '&job_id=' + $scope.jobId).then(function(successResponse){
$scope.return = successResponse;
$scope.data = $scope.return.data.data;
}, function(errorRepsonse){
$scope.error = errorRepsonse;
});
$scope.doRefresh = function() {
$http.get('https://domain/api/job?token=' + localStorage.getItem("token") + '&job_id=' + $scope.jobId).then(function(successResponse){
$scope.return = successResponse;
$scope.data = $scope.return.data.data;
$scope.$broadcast('scroll.refreshComplete');
}, function(errorRepsonse){
$scope.error = errorRepsonse;
});
};
})
The view which displays the people results. 显示人员结果的视图。
<ion-refresher
pulling-text="Pull to refresh..."
on-refresh="doRefresh()">
</ion-refresher>
<ion-nav-back-button class="button-clear">
<i class="ion-arrow-left-c"></i> Back
</ion-nav-back-button>
<ion-list>
<ion-item class="item-remove-animate item-icon-right" ng-repeat="person in data" type="item-text-wrap">
<h2>{{person.name }}</h2>
</ion-item>
</ion-list>
You can use resolve 您可以使用解决
.state('job', {
url: '/job/:jobID',
templateUrl: 'templates/job.html',
controller: 'jobCtrl',
resolve: {
job: function($stateParams, $http){
return $http.get(...).then(function(resp){
...
// return job data
});
}
}
})
...
And then in your controller 然后在您的控制器中
.controller('jobCtrl', function($scope, job, $http, $state) {
$scope.job = job;
Your controller will only run after job
is resolved. 您的控制器将仅在解决job
后运行。
Also, you can display a loader while loading 另外,您可以在加载时显示加载器
Update 更新资料
To make things clear: $http.get()
returns a promise. 明确说明: $http.get()
返回一个promise。 Once the promise is resolved then your controller function will be executed. 诺言一旦解决,您的控制器功能便会执行。 Eventually, the $http.get
should return the data that you want to use inside your controller (in this case- the job info). 最终, $http.get
应该返回您要在控制器内部使用的数据(在本例中为作业信息)。
This way you don't need to start fetching information asynchronously inside your controller. 这样,您就无需开始在控制器内部异步获取信息。 Your controller starts with the data and should be kept simple. 您的控制器以数据开头,应保持简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.