显示新页面之前函数的绑定值
Binding Value of function before showing new page angularjs
我试图将一个变量绑定到一个范围之前,它移动到视图,但我的视图显示之前,变量是有界的。这是我的代码。
$scope.getListing = function() {
var deferred = $q.defer();
$scope.$applyAsync(function() {
$rootScope.listingDetails =[];
referralCasesGroupByCaseStatus.getListing($rootScope.userDetails.rows.item(2).value).then(function(data){
$rootScope.listingDetails = data
deferred.resolve($rootScope.listingDetails)
if($rootScope.fromDashboard === false) {
$scope.showCaseStatus(1);
$state.go('app.case_status')
}
else {
$scope.showCaseStatus($rootScope.statusNumber)
$state.go('app.case_status')
$ionicLoading.hide();
}
});
})
return deferred.promise;
};
var changedNumber = 0;
$scope.showCaseStatus = function(number) {
var finishedPushingListings = false;
$rootScope.listingByCaseStatus = [];
$rootScope.caseStatusListings = [];
if(changedNumber !== 0 && changedNumber !== number) {
changedNumber = number;
}
else {
if(changedNumber > 0) {
$scope.$applyAsync($rootScope.detailsPresent = true);
}
}
$scope.$applyAsync(function() {
angular.forEach($rootScope.listingDetails, function(value, key) {
if(value.real_estate_agent_assignment_status_id == number) {
$rootScope.listingByCaseStatus.push(value);
}
});
})
$scope.$applyAsync(function() {
if($rootScope.listingByCaseStatus == 0 || $rootScope.listingByCaseStatus == undefined || $rootScope.listingByCaseStatus == null) {
$rootScope.detailsPresent = true;
$rootScope.changeNumber = true;
finishedPushingListings = true;
}
else {
$rootScope.detailsPresent = false;
$scope.noMoreItemsAvailable = false;
$rootScope.changeNumber = true;
finishedPushingListings = true;
}
})
};
这里的主要问题是函数$scope.showCaseStatus($rootScope.statusNumber)
在执行$state.go('app.case_status')
之前没有完成执行,我希望它在跳转到$state.go('app.case_status')
之前等待并完成执行。
任何帮助都是感激的。
由于您使用的是$applyAsync()
,所以函数效果是异步的。实现您想要的一种方法是使showCaseStatus()
返回一个承诺-并考虑到有2个异步块:
$scope.showCaseStatus = function(number) {
var ..., d1, d2;
...
d1 = $q.defer();
$scope.$applyAsync(function() {
angular.forEach($rootScope.listingDetails, function(value, key) {
...
});
d1.resolve();
})
d2 = $q.defer();
$scope.$applyAsync(function() {
...
d2.resolve();
})
// both promises must be resolved to continue
return $q.all([d1.promise, d2.promise]);
};
则调用者变为:
$scope.showCaseStatus($rootScope.statusNumber).then(function() {
$state.go('app.case_status')
$ionicLoading.hide();
});
一些注意事项:
- 如果你不需要异步块,你可以删除它们并简化代码
- 如果第二个异步块依赖于第一个异步块的结果,它们也应该同步
相关文章:
- 如何在动态创建的节点上绑定函数
- 绑定函数时在IE7中未实现Javascript错误
- jQuery结合了2个绑定函数(类型错误:a是未定义的)
- Jquery函数无法处理动态加载的内容(不是绑定函数)
- 从console.log调用webpack绑定函数/类
- React没有绑定函数调用,尽管有相反的警告
- jQuery使用最后一个参数在Javascript循环中单击绑定函数
- 如何在React Native中为ListView中的每个元素绑定函数
- jquery绑定函数不起作用
- 具有绑定与未绑定函数的数组筛选器
- 绑定函数仅适用于最后一个元素
- 如何将新函数绑定到元素的 onclick 事件,并使其在所有其他绑定函数之前运行
- 绑定函数链接
- 动态 JS 拖动绑定函数限制在线拖动
- 绑定函数的返回语句中参数的推理是什么
- 在jQuery上动态绑定函数
- 确定 JavaScript 函数是否为绑定函数
- jQuery 绑定函数不起作用
- 反应组件中的绑定函数
- Javascript 绑定函数到 Ajax 加载事件