我认为您可以尝试使用角度的 $timeout 来定期轮询服务器中的数据(例如在您的情况下为 30 分钟或更短)
由于没有您的数据就无法创建示例,而且我不想停留 30 分钟以查看它是否有效,所以我为您做了这个小提琴:http: //jsfiddle.net/DotDotDot/mgSWt/3 /,它模拟数据的查询(你用 $http 做什么)然后分析它以便只显示时间限制内的项目
更准确地说,我在控制器的开头创建了 3 个变量,分别对应于页面加载的时间,一分钟和两分钟前(但很容易将其传递到 30 分钟):
var n=new Date()
var onemin=n.getTime()-60*1000//one min from now
var twomin=n.getTime()-60*1000*2//two min from now
之后,我创建了一个函数来获取数据(以您的示例为例,我认为使用服务可能会更好,但这是另一个问题)。此函数将获取整个数据(至少在我的示例中对其进行硬编码)并检查要显示的项目。然后我调用这个函数,函数本身安排下一个执行时间,在我的例子中,每秒(你应该设置为另一个值,30 分钟,更少,这取决于你的应用程序):
$scope.updateData=function(){
$timeout(function(){
//$http.get('/your/url').success(...).error(...)
//you should get your data here, but I just force it for the example
$scope.rawData=[{"time":n.getTime(),"content":"The value created when the page loaded"},
{"time":onemin,"content":"The value was created one minute before the page load"},
{"time":twomin,"content":"The value was created two minute before the page load"}];
console.log($scope.rawData)
var whatTimeIsIt=new Date();
$scope.data=Array()//erasing any previous data
$scope.notShown=Array();//for the test
for(var i in $scope.rawData){
if($scope.rawData[i].time>(whatTimeIsIt.getTime() - 90*1000))//checking each value time, if there was more than 1 min 30 sec or not
{
$scope.data.push($scope.rawData[i]);
}
else
{
$scope.notShown.push($scope.rawData[i])
}
}
//at the end of the function, we populated $scope.data with all the object with a time less than 1min 30 ago
$scope.updateData();//schedule the next update
}
,1000);
}
$scope.updateData();
该代码仅用于这个精确的示例,因此您必须对其进行调整,但该概念有效。在 HTML 方面,您当然可以显示值,我在示例中分两部分进行了显示,应该显示的数据(在时间限制内)和应该被跳过的数据(>时间限制)
Values that should be shown :
<div ng-repeat='values in data'>
{{values.time}} : {{values.content}}
</div>
<br/>
Values outdated :
<div ng-repeat='values in notShown'>
{{values.time}} : {{values.content}}
</div>
如果你观察控制台,你可以看到对函数的每一次调用,每一秒。
在这个例子中,我将时间限制设置为 1min30,所以前 2 个对象将在显示列表中显示 30 秒,31 秒后,仅显示第一个对象再显示 1 分钟,然后将它们全部隐藏/删除/发送到魔多/随便你
希望这有帮助,祝你好运