I have following code trying to display time from a date
我有以下代码试图从日期显示时间。
<div ng-repeat="item in items">
<input type="text" ng-model="item.name" />
<input ng-model="item.currentDate" />
</div>
The controller code is
控制器代码
function MyCtrl($scope, $filter) {
$scope.item.currentDate = new Date();
$scope.$watch('currentDate', function(date){
$scope.currentDate = $filter('date')(date, 'shortTime');
});
It doesnt work. I have a working plunker for the same code without ng-repeat in it. http://jsfiddle.net/HB7LU/4108/
它不工作。我有一个在没有ng重复的相同代码的工作插入。http://jsfiddle.net/HB7LU/4108/
Please let me know how I can this filter to display time inside ng-repeat Thanks
请告诉我这个过滤器如何在ng-repeat中显示时间,谢谢。
2 个解决方案
#1
2
I assume you already have an array of items that you are looping over. Something like this:
我假设您已经有了一个循环的项目数组。是这样的:
$scope.items = [{
name: 'date1',
currentDate: new Date()
}, {
name: 'date2',
currentDate: new Date()
}];
You need to setup your $watch
on that array:
您需要在该数组上设置$watch:
$scope.$watch('items', function (newValue, oldValue) {
angular.forEach(newValue, function(item){
item.currentDate = $filter('date')(item.currentDate, 'shortTime');
});
}, true);
JSFiddle
#2
0
Inside your controller you should setup an array of items:
在你的控制器内部,你应该设置一系列的项目:
$scope.items = [];
$scope.items.push({ name:'...', currentDate:new Date(...)});
#1
2
I assume you already have an array of items that you are looping over. Something like this:
我假设您已经有了一个循环的项目数组。是这样的:
$scope.items = [{
name: 'date1',
currentDate: new Date()
}, {
name: 'date2',
currentDate: new Date()
}];
You need to setup your $watch
on that array:
您需要在该数组上设置$watch:
$scope.$watch('items', function (newValue, oldValue) {
angular.forEach(newValue, function(item){
item.currentDate = $filter('date')(item.currentDate, 'shortTime');
});
}, true);
JSFiddle
#2
0
Inside your controller you should setup an array of items:
在你的控制器内部,你应该设置一系列的项目:
$scope.items = [];
$scope.items.push({ name:'...', currentDate:new Date(...)});