如何让我的模型在 60 秒后自动更新?
我在 $scope.tweets
中有一个数组,其中包含推文列表。我希望 $scope.currentTweet
每 60 秒从数组中弹出一条推文。
有没有一种特殊的方法可以用 Angular 来做到这一点?
最佳答案
是的,你可以。 AngularJS 为您提供了 $interval。
- 将 $interval 注入(inject)您的 Controller 。
- 注册它以每 60 秒调用一次更新函数。
示例:
myApp.controller('MyCtrl', ['$scope', '$interval', function($scope, $interval){
$scope.tweets = [];
$scope.currentTweet = null;
var myUpdater = $interval(function(){
$scope.currentTweet = $scope.tweets.pop();
}, 60*1000);
//And you can cancel the interval anytime you want by calling this:
$interval.cancel(myUpdate);
})
关于javascript - 60 秒后更新 Angular 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33381780/