我想在我的表中包含新条目后立即更新我的列表。 尝试使用$ interval服务,但在回调后控制器没有加载。
$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;
}
答案 0 :(得分:-1)
**使用incapsulation升级的答案(safeapply)**
尝试使用此安全应用程序:
$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();