我试图过滤一个纳克重复生成的列表,其中包含来自服务器的毫秒格式的日期对象。列表生成精细,我用角日期格式化它,但当我过滤时,我假设过滤器在毫秒内工作,而不是文本。在显示日期以使过滤器正常工作之前,是否需要格式化控制器或服务中的日期,还是有办法解决这个问题?
html:
<!DOCTYPE html>
<html ng-app="dates">
<head>
<script data-require="angular.js@*" data-semver="1.3.0-beta.5" src="https://code.angularjs.org/1.3.0-beta.5/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body ng-controller="DateController">
<input type="text" class="form-control" placeholder="Search" ng-model="searchText">
<p>{{dates}}</p>
<ul ng-repeat="record in records | filter: searchText">
<li>{{record.activity}}</li>
<li>{{record.date | date: longDate}}</li>
</ul>
</body>
</html>这是我的javascript:
angular.module('dates', []);
var DateController = function($scope) {
$scope.records = [{date:1403802879000, activity: "biking"}, {date:1276495200000, activity: "hiking"}, {date:1403802879000, activity: "walking"},
{date:1403802879000, activity: "biking"}, {date:1276495200000, activity: "hiking"}];
}下面是一个柱塞:http://plnkr.co/byQkzHSTXLHPCdU0pj3o
谢谢你的建议!
发布于 2014-07-10 21:47:36
没错,ng过滤器将查看$scope.records数组并根据输入的表达式返回一个新数组(在本例中为searchText),这是正确的。
简单的解决方案,只需将日期转换为$scope.records中的字符串即可。
硬解决方案,您可以编写自己的ng过滤器模块。09 https://github.com/angular/angular.js/blob/master/src/ng/filter/filter.js#L3
https://stackoverflow.com/questions/24686679
复制相似问题