繁体   English   中英

如何从数据库刷新列表而不刷新页面

[英]How to refresh List from Database without refreshing the Page

我试图在表中包含新条目后立即更新列表。 使用$ 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM