Well, I want to show an input date field but with the format changed.
I mean, the default format is: yyyy-MM-dd (2015-11-20), but I want it: (11-20-2015).
There's a jsfiddle I found with momentjs (without angular) and works exactly as I want.
Working with AngularJs, I have done too with a directive, but with a span field (and works). But if I do with an input date, it doesn't works.
This is the 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>
And the 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);
Here is the Plunker. Some ideas?