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

Displaying time from date in ng-repeat Angularjs

本文关键字:日期 开始 时间 显示 ng-repeat Angularjs      更新时间:2023-09-26

我有以下代码试图从日期

显示时间
<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');
    });

行不通。我有一个工作柱塞相同的代码没有重复在它。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

你应该在你的控制器中设置一个项目数组:

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