可能需要创建自己的服务来做到这一点。
类似的东西(伪代码,因为我没有服务器来支持这些)......
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!');
});