在ng-repeat Angularjs中显示从日期开始的时间

戴维森

我有以下代码尝试显示日期的时间

<div ng-repeat="item in items">
<input type="text" ng-model="item.name" />
<input ng-model="item.currentDate" />
</div>

控制器代码为

function MyCtrl($scope, $filter) {
    $scope.item.currentDate = new Date();
    $scope.$watch('currentDate', function(date){
        $scope.currentDate = $filter('date')(date, 'shortTime');
    });

它不起作用。我有一个工作正常的代码,没有ng-repeat。http://jsfiddle.net/HB7LU/4108/

请让我知道如何使用此过滤器在ng-repeat中显示时间谢谢

耶拉德

我假设您已经有一组要遍历的项目。像这样的东西:

$scope.items = [{
    name: 'date1',
    currentDate: new Date()
}, {
    name: 'date2',
    currentDate: new Date()
}];

您需要$watch在该阵列上进行设置

$scope.$watch('items', function (newValue, oldValue) {
    angular.forEach(newValue, function(item){
        item.currentDate = $filter('date')(item.currentDate, 'shortTime');
    });
}, true);

JSFiddle

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章