I have a list that registers the previous names that I have selected, as shown below. I want the list to always show only the last 5 results, however, what I have at the moment shows the first 5 in order of time with the last first. How can this be achieved?
http://jsfiddle.net/sfqo2fn3/1/
<div ng-controller="MyCtrl"> <input type="text" ng-model="updatedname" /> <input type="button" value="Change name" ng-click="changeName(updatedname)"/> <br/> Hello, {{name}}! <ul> <li ng-repeat="name in nameLog | limitTo:5 | orderBy:'time':true">{{name.value}} - {{name.time}}</li> </ul> </div> var myApp = angular.module('myApp',[]); myApp.factory('UserService', function() { var _nameLog = []; var userService = {}; userService.name = "John"; userService.ChangeName = function (value) { userService.name = value; }; userService.logName = function (value) { _nameLog.push ({ "value":value, "time" :Date.now() }); }; userService.getNameLog = function(){ return _nameLog; } return userService; }); function MyCtrl($scope, UserService) { $scope.name = UserService.name; $scope.updatedname=""; $scope.changeName=function(data){ $scope.updateServiceName(data); } $scope.updateServiceName = function(name){ UserService.ChangeName(name); UserService.logName(name); $scope.name = UserService.name; $scope.nameLog = UserService.getNameLog(); } }
javascript angularjs angularjs-ng-repeat
ak85
source share