我是棱角分明的新手.我想在几分钟后使用$timeout of angular来刷新范围.我正在开发一个社交应用程序,我需要在几分钟后刷新通知范围.使用服务从http请求获取通知.
JS:
App.factory('MyService',function($scope,$timeout){ return{ notification:return function(callback){ $timeout(function(){ $http.get("notification/get").success(callback) },100000); } }); function Controller($scope,MyService){ MyService.notification(function(result){ $scope.notification =data; });
}
现在我怎么能在几分钟后做出http请求,1分钟后刷新通知范围.我尝试使用$timeout但事情并不顺利.
但我建议将$interval移动到控制器.
原文链接:https://www.f2er.com/angularjs/143416.htmlApp.factory('MyService',$timeout){ return{ notification: function(){ return $http.get("notification/get").success(function(response){ return response.data; }); } }); function Controller($scope,MyService,$interval){ /** * Loads and populates the notifications */ this.loadNotifications = function (){ MyService.notification().then(function(data){ $scope.notification =data; }); }); //Put in interval,first trigger after 10 seconds var theInterval = $interval(function(){ this.loadNotifications(); }.bind(this),10000); $scope.$on('$destroy',function () { $interval.cancel(theInterval) }); //invoke initialy this.loadNotifications(); }
这似乎是一个更好的架构.