I am a newbie to angular. I want to use $timeout of angular to refresh scope after few minutes. I am working on an social app where i need to refresh notification scope after few minutes. Getting notification from a http request using service.
我是棱角分明的新手。我想在几分钟后使用$ timeout of angular来刷新范围。我正在开发一个社交应用程序,我需要在几分钟后刷新通知范围。使用服务从http请求获取通知。
JS:
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;
});
}
}
Now how can i make http request after few minutes let'say 1 minute and refresh notification scope. I tried using $timeout but things are not working fine.
现在我怎么能在几分钟后做出http请求,1分钟后刷新通知范围。我尝试使用$ timeout但事情并不顺利。
2 个解决方案
#1
28
But i would suggest to move the $interval
to the controller.
但我建议将$ interval移动到控制器。
App.factory('MyService' ,function($scope,$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();
}
This seems like a better architecture there.
这似乎是一个更好的架构。
Passing, resolving or rejecting promises will $digest
the scope. You want to get the notifications every x milliseconds and pass them into the scope.
传递,解决或拒绝承诺将消化范围。您希望每隔x毫秒获取通知并将其传递到范围中。
#2
1
You want to instantiate the timeout after it finishes. Try changing your $timeout function to something like:
您希望在完成后实例化超时。尝试将$ timeout函数更改为:
var timer = $timeout( function refresh(){
$http.get("notification/get").success(callback)
timer = $timeout(refresh, 100000);
}, 100000);
#1
28
But i would suggest to move the $interval
to the controller.
但我建议将$ interval移动到控制器。
App.factory('MyService' ,function($scope,$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();
}
This seems like a better architecture there.
这似乎是一个更好的架构。
Passing, resolving or rejecting promises will $digest
the scope. You want to get the notifications every x milliseconds and pass them into the scope.
传递,解决或拒绝承诺将消化范围。您希望每隔x毫秒获取通知并将其传递到范围中。
#2
1
You want to instantiate the timeout after it finishes. Try changing your $timeout function to something like:
您希望在完成后实例化超时。尝试将$ timeout函数更改为:
var timer = $timeout( function refresh(){
$http.get("notification/get").success(callback)
timer = $timeout(refresh, 100000);
}, 100000);