解决angularjs有时无法刷新数据的情况

解决angularjs有时无法刷新数据的情况,控制器中添加下列函数;当遇到异步请求等情况导致的数据无法刷新,调用下列函数

$scope.safeApply = function(fn) {
var phase = this.$root.$$phase;
if (phase == '$apply' || phase == '$digest') {
if (fn && (typeof(fn) === 'function')) {
fn();
}
} else {
this.$apply(fn);
}

};  

例如:

 app.controller('myCtrl', function($scope, $http) {

$scope.safeApply = function(fn) {
var phase = this.$root.$$phase;
if (phase == '$apply' || phase == '$digest') {
if (fn && (typeof(fn) === 'function')) {
fn();
}
} else {
this.$apply(fn);
}
}; 

$scope.setDefault= function(x){

$http({url :"......",

method : 'POST',
responseType : 'json',
data : {....}
}).success(function(response) {

$scope.safeApply($scope.getList());

});

$scope.getList  = function(){

...........................

};

});

展开阅读全文

没有更多推荐了,返回首页