Although you can create a stateful filter, the documentation strongly advises that you create only stateless and idempotent filters. That is why the directive is the best idea here.
The principle is simple: when the command is compiled, create a timer that will update the value every minute. The $interval great for this purpose. However, as indicated in the documentation, be sure to destroy it when you no longer need it.
MyApp.directive('relativeTime', function ($interval) { return { scope: { time: '=', }, template: '<div>{{relativeTime}} secondes ago.</div>', link: function ($scope) { $scope.relativeTime = 0; var intervalPromise = $interval(function () { ++$scope.relativeTime; }, 1000); $scope.$watch('time', function (time) { var currentTime = new Date(); $scope.relativeTime = Math.round((currentTime.getTime() - time.getTime()) / 1000); }); $scope.$on('$destroy', function () { $interval.cancel(intervalPromise); }); }, }; });
Fiddle
For demonstration purposes, this directive is very simple and is updated every second. But itβs easy to adapt it to your use case and, in particular, to your date.fromNow() function.
source share