Reputation: 57
How can I refresh a angularjs directive every x seconds ? I want to reload function attached to link ever x seconds, regards.
Upvotes: 0
Views: 133
Reputation: 345
Use an interval in your directive. But remember to destroy the interval when the directive is destroyed:
angular.directive('myDirective', myDirective);
myDirective.$inject = ['$interval'];
function myDirective($interval) {
var i = undefined;
return {
restrict: 'A',
link: function(scope) {
i = $interval(function () {
// Do stuff here.
}, seconds * 1000);
scope.$on('$destroy', function() {
console.log("destroy");
if (angular.isDefined(i)) {
$interval.cancel(i);
i = undefined;
}
});
}
};
}]);
Upvotes: 2