While jvandemo's answer will work, I think it can be improved slightly. By using setInterval
, it breaks the dependency injection convention that Angular follows and makes unit testing of the controller difficult.
Angular doesn't currently support setInterval
through its built-in services, but you can use the $timeout
service to produce the same functionality. I'd change the controller to this:
app.controller('MainCtrl', function($scope, $http, $timeout) {
// Function to get the data
$scope.getData = function(){
$http.get('style.css')
.success(function(data, status, headers, config) {
// Your code here
console.log('Fetched data!');
});
};
// Function to replicate setInterval using $timeout service.
$scope.intervalFunction = function(){
$timeout(function() {
$scope.getData();
$scope.intervalFunction();
}, 1000)
};
// Kick off the interval
$scope.intervalFunction();
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…