dnasir / angular-dateParser

A simple parser that converts date and time strings into Date object
http://dnasir.github.io/angular-dateParser/
MIT License
38 stars 25 forks source link

$locale change at runtime #32

Closed ardf69 closed 8 years ago

ardf69 commented 9 years ago

The module doesn't work if the $locale change at runtime. I changed the following code:

} ]).factory("$dateParser", [ "$locale", "dateParserHelpers", function($locale, dateParserHelpers) {
    "use strict";
    var datetimeFormats = $locale.DATETIME_FORMATS;
    var monthNames = datetimeFormats.MONTH.concat(datetimeFormats.SHORTMONTH);
    var dayNames = datetimeFormats.DAY.concat(datetimeFormats.SHORTDAY);
    return function(val, format) {
        if (angular.isDate(val)) {
            return val;
        }

in

} ]).factory("$dateParser", [ "$locale", "dateParserHelpers", function($locale, dateParserHelpers) {
    "use strict";
    return function(val, format) {
        var datetimeFormats = $locale.DATETIME_FORMATS;
        var monthNames = datetimeFormats.MONTH.concat(datetimeFormats.SHORTMONTH);
        var dayNames = datetimeFormats.DAY.concat(datetimeFormats.SHORTDAY);

        if (angular.isDate(val)) {
            return val;
        }

and

angular.module("dateParser").directive("dateParser", [ "dateFilter", "$dateParser", function(dateFilter, $dateParser) {
    "use strict";
    return {
        restrict: "A",
        require: "ngModel",
        link: function(scope, element, attrs, ngModel) {
            var dateFormat;
            attrs.$observe("dateParser", function(value) {
                dateFormat = value;
                ngModel.$render();
            });
            ngModel.$parsers.unshift(function(viewValue) {

in

angular.module("dateParser").directive("dateParser", [ "dateFilter", "$dateParser", "$locale", function(dateFilter, $dateParser, $locale) {
    "use strict";
    return {
        restrict: "A",
        require: "ngModel",
        link: function(scope, element, attrs, ngModel) {
            var dateFormat;
            attrs.$observe("dateParser", function(value) {
                dateFormat = value;
                ngModel.$render();
            });
            scope.$watch(
                function() {
                    return $locale.id;
                },
                function(newValue, oldValue, scope) {
                    if (angular.isDefined(dateFormat)) {
                     ngModel.$render();
                    }
                }
            );
            ngModel.$parsers.unshift(function(viewValue) {

Ciao Angelo

dnasir commented 9 years ago

@ardf69 Yea, I've been wanting to merge a pull request that addresses this issue. Just haven't had the time. Now I have a reason to actually get it done ;-)

dnasir commented 8 years ago

This has been resolved in the latest version, albeit still in beta.