我尝试在表中包含新条目后立即更新列表。
已尝试使用$ 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;
}
最佳答案
**更新了封装的答案(安全应用)**
尝试使用此安全应用技巧:
$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();
参考:https://coderwall.com/p/ngisma/safe-apply-in-angular-js