如何使用AngularJS实现自动刷新模块

时间:2017-03-02 02:33:25

标签: javascript angularjs ionic-framework timeago

好吧,我是从Angularjs开始的,所以我可以适当地不清楚我的问题。

我正在使用名为ngtimeago的模块(下方),并且我尝试进行自动刷新,以更新所有{{date | timeago}}

如何使用现有的.filter()进行全局刷新?



 'use strict';
 var catalyst = angular.module('ngtimeago', []);
catalyst.filter('timeago', function($timeout) {
        return function(input, p_allowFuture) {
		            var substitute = function (stringOrFunction, number, strings) {
                    var string = angular.isFunction(stringOrFunction) ? stringOrFunction(number, dateDifference) : stringOrFunction;
                    var value = (strings.numbers && strings.numbers[number]) || number;
                    return string.replace(/%d/i, value);
                },
                nowTime = (new Date()).getTime(),
                date = (new Date(input)).getTime(),
                refreshMillis= 6e4, //A minute
                allowFuture = p_allowFuture || false,
                strings= {
                     prefixAgo: '',
                    prefixFromNow: '',
                    suffixAgo: "ago",
                    suffixFromNow: "from now",
                    seconds: "%d seconds",
                    minute: "about a minute",
                    minutes: "%d minutes",
                    hour: "about an hour",
                    hours: "about %d hours",
                    day: "a day",
                    days: "%d days",
                    month: "about a month",
                    months: "%d months",
                    year: "about a year",
                    years: "%d years"
                },
                dateDifference = nowTime - date,
                words,
                seconds = Math.abs(dateDifference) / 1000,
                minutes = seconds / 60,
                hours = minutes / 60,
                days = hours / 24,
                years = days / 365,
                separator = strings.wordSeparator === undefined ?  " " : strings.wordSeparator,
                prefix = strings.prefixAgo,
                suffix = strings.suffixAgo;
                            if (allowFuture) {
                if (dateDifference < 0) {
                    prefix = strings.prefixFromNow;
                    suffix = strings.suffixFromNow;
                }
            }
            words = seconds < 45 && substitute(strings.seconds, Math.round(seconds), strings) ||
            seconds < 90 && substitute(strings.minute, 1, strings) ||
            minutes < 45 && substitute(strings.minutes, Math.round(minutes), strings) ||
            minutes < 90 && substitute(strings.hour, 1, strings) ||
            hours < 24 && substitute(strings.hours, Math.round(hours), strings) ||
            hours < 42 && substitute(strings.day, 1, strings) ||
            days < 30 && substitute(strings.days, Math.round(days), strings) ||
            days < 45 && substitute(strings.month, 1, strings) ||
            days < 365 && substitute(strings.months, Math.round(days / 30), strings) ||
            years < 1.5 && substitute(strings.year, 1, strings) ||
            substitute(strings.years, Math.round(years), strings);
		//  	console.log(prefix+words+suffix+separator);
			prefix.replace(/ /g, '')
			words.replace(/ /g, '')
			suffix.replace(/ /g, '')
			return (prefix+' '+words+' '+suffix+' '+separator);
             };
    var refreshDates = function () {
        $timeout(refreshDates, 3000);
			 console.log("Atualizando datas...");
			// $filter('timeago').reload();
    };
        });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<html ng-app="App">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
			<script src="js/angular.min.js" type="text/javascript"></script>
			<script type="text/javascript">
      
			angular.module('App',['ngtimeago']).controller('TestTimeago', function($scope){

          $scope.listTest = [];
var tomorrow = new Date();

		      $scope.test1 = { 
							"date": Date.now()
							 }
		    	$scope.test2 = { 
							"date": tomorrow.setDate(tomorrow.getDate() -0.002)

							 }
		    $scope.listTest.push($scope.test1);
		    $scope.listTest.push($scope.test2);
        
		});
	
			</script>
      <body ng-controller="TestTimeago">
             <ul>    
                 <li ng-repeat='time in listTest'>
                      <p>{{ time.date | timeago}}</p>
                </li>
            </ul>
      </body>
&#13;
&#13;
&#13;

0 个答案:

没有答案