Javascript 如何在不刷新页面的情况下从数据库刷新列表
我正在尝试在我的表中包含新条目后立即更新我的列表。 尝试使用$interval服务,但在回调后,控制器未加载Javascript 如何在不刷新页面的情况下从数据库刷新列表,javascript,angularjs,ajax,Javascript,Angularjs,Ajax,我正在尝试在我的表中包含新条目后立即更新我的列表。 尝试使用$interval服务,但在回调后,控制器未加载 $scope.displayNotification=function(){ NavbarService.displayNotification() .then(function(response){ angular.forEach(response,function(val,key){
$scope.displayNotification=function(){
NavbarService.displayNotification()
.then(function(response){
angular.forEach(response,function(val,key){
if(key == 'success'){
$scope.notificationList= val;
alert("Hi");
console.log($scope.notificationList);
if(null != $scope.notificationList && undefined !=$scope.notificationList){
$scope.notificationCount = $scope.notificationList.length;
}
}
});
},function(){
// console.error("Error while fetching Notification");
});
}
用于在间隔后回调函数
intervalPromise = $interval($scope.displayNotification, 3000);
调用此函数从我的表中获取数据
这是http调用
function displayNotification() {
var defer = $q.defer();
var url='api/notify';
$http({
method : "POST",
url : url,
data : {}
}).success( function(response, status,config, headers) {
defer.resolve(response);
}).error(function(errResp) {
defer.reject({ message:"No Notification Details" });
});
return defer.promise;
}
**带封装的更新答案(安全适用)** 尝试使用此安全应用程序:
$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.safeApply(function() {
$scope.notificationCount = $scope.notificationList.length;
});
$scope.notificationCount = $scope.notificationList.length;
$scope.safeApply();
或者像这样:
$scope.safeApply(function() {
$scope.notificationCount = $scope.notificationList.length;
});
$scope.notificationCount = $scope.notificationList.length;
$scope.safeApply();
参考:否!没有帮助。在控制台中获取此错误。错误:[$rootScope:inprog]$digest已在进行中我现在尝试更新答案,它与safeapply一起工作吗?如果有效,请向上投票:)