diff --git a/angular-moment.js b/angular-moment.js index b1925c0..ddb2703 100644 --- a/angular-moment.js +++ b/angular-moment.js @@ -173,11 +173,14 @@ var localDate = new Date().getTime(); var preprocess = angularMomentConfig.preprocess; var modelName = attr.amTimeAgo; + var currentFrom; var isTimeElement = ('TIME' === element[0].nodeName.toUpperCase()); function getNow() { var now; - if (amTimeAgoConfig.serverTime) { + if (currentFrom) { + now = currentFrom; + } else if (amTimeAgoConfig.serverTime) { var localNow = new Date().getTime(); var nowMillis = localNow - localDate + amTimeAgoConfig.serverTime; now = moment(nowMillis); @@ -256,6 +259,17 @@ updateMoment(); }); + if (angular.isDefined(attr.amFrom)) { + scope.$watch(attr.amFrom, function (value) { + if ((typeof value === 'undefined') || (value === null) || (value === '')) { + currentFrom = null; + } else { + currentFrom = moment(value); + } + updateMoment(); + }); + } + if (angular.isDefined(attr.amWithoutSuffix)) { scope.$watch(attr.amWithoutSuffix, function (value) { if (typeof value === 'boolean') { diff --git a/tests.js b/tests.js index 044c2f9..dd617ec 100644 --- a/tests.js +++ b/tests.js @@ -312,6 +312,17 @@ describe('module angularMoment', function () { expect(element.text()).toBe('2013-12-15'); }); }); + + describe('am-from attribute', function() { + it('should make the calculations from the am-from given', function() { + $rootScope.from = new Date(2015, 6, 11); + $rootScope.testDate = new Date(2015, 6, 12); + var element = angular.element(''); + element = $compile(element)($rootScope); + $rootScope.$digest(); + expect(element.text()).toBe('in a day'); + }); + }); }); describe('am-without-suffix attribute', function () {