【发布时间】:2015-11-20 17:40:34
【问题描述】:
好吧,我想显示一个输入日期字段,但格式已更改。
我的意思是,默认格式是:yyyy-MM-dd (2015-11-20),但我想要它:(11-20-2015)。
我用momentjs(没有角度)找到了一个jsfiddle,它可以完全按照我的意愿工作。
使用 AngularJs,我也使用了指令,但使用了 span 字段(并且有效)。但如果我使用输入日期,它就不起作用。
这是html:
<body ng-app="docsTimeDirective">
<div ng-controller="Controller">
Current date is: <span my-current-time="format"></span><hr/>
<input type="date" ng-model="myDate.value" my-current-time="format">
</div>
</body>
还有Js:
(function(angular) {
'use strict';
angular.module('docsTimeDirective', [])
.controller('Controller', ['$scope', function($scope) {
$scope.format = 'MM/dd/yyyy';
$scope.myDate = {
value: new Date()
};
}])
.directive('myCurrentTime', ['$interval', 'dateFilter', function($interval, dateFilter) {
function link(scope, element, attrs) {
var format,
timeoutId;
function updateTime() {
element.text(dateFilter(new Date(), format));
console.log(dateFilter(new Date(), format));
}
scope.$watch(attrs.myCurrentTime, function(value) {
format = value;
updateTime();
});
element.on('$destroy', function() {
$interval.cancel(timeoutId);
});
// start the UI update process; save the timeoutId for canceling
timeoutId = $interval(function() {
updateTime(); // update DOM
}, 1000);
}
return {
link: link
};
}]);
})(window.angular);
这里是Plunker。有什么想法吗?
【问题讨论】:
标签: javascript html angularjs angularjs-directive