아마도 자신의 서비스를 만들어야 할 것입니다.
(사이비 코드, 나는이 최대 중 하나를 백업하는 서버가 없기 때문에) 같은
뭔가 ...
app.factory('andrejsSuperAwesomeService', ['$cacheFactory', '$http', function($cacheFactory, $http) {
//get your cache ready.
var userCache = $cacheFactory('users');
// start an interval to check for data.
// TODO: add a public function to turn this on and off.
setInterval(function(){
//check for changes to the data.
$http.get('/Get/New/User/Changes')
.success(function(changes) {
if(!changes) return;
//we'll assume we get some collection of changes back,
// with some change type and the user data.
for(var i = 0; i < changes.length; i++) {
var change = changes[i];
switch(change.changeType) {
case 'delete':
// okay just remove the deleted ones.
userCache.remove(change.user.id);
break;
case 'addUpdate':
// if it's added or updated, let's just
// remove and re-add it, because we can't know what
// we already have or don't have.
userCache.remove(change.user.id);
userCache.put(chnage.user.id, change.user);
break;
}
}
});
}, 10000); // every 10 secs
return {
users: {
//a function to get a user.
get: function(userId, callback) {
var user = userCache.get(userId);
if(!user) {
//the user is not in the cache so let's get it.
$http.get('/Uri/To/Get/A/User?userId=' + userId)
.success(function(data) {
//great, put it in the cache and callback.
userCache.put(userId, data);
if(callback) callback(data);
});
} else {
//we already have the user, callback.
if(callback) callback(data);
}
}
}
};
});
그런 다음 컨트롤러에 당신이 당신의 서비스를 주입하는 것 등처럼 사용 : 내가 볼 수 있기 때문에
andrejsSuperAwesomeService.users.get(12345, function(user) {
//do something with user here.
alert(user.name + ' is a naughty user!');
});
이 서비스 사용을 만들 수는 당신이 그것을 집어됩니다 기대보다는 $ 범위를 사용할 수 있도록 $ 간격 지침을 angulars – DrogoNevets