Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignoring model changes if model is set to string #305

Open
easyest opened this issue Feb 25, 2021 · 1 comment
Open

Ignoring model changes if model is set to string #305

easyest opened this issue Feb 25, 2021 · 1 comment

Comments

@easyest
Copy link

easyest commented Feb 25, 2021

Input:

                <input class="form-control"
                       name="MomentPicker"
                       ng-model="vm.testDateTime"
                       ng-model-options="{ updateOn: 'blur' }"
                       moment-picker="vm.testDateTime">

If model is set to string - it is ignored. See Plunkr

I have no TS coding experience, so I made changes to JS directly. See Plunkr

The change I made is adding one more watcher after the watcher if ($attrs['ngModel'] != $attrs['momentPicker']):

                $scope.$watch('model', function (newValue, oldValue) {
                    if (typeof newValue === "string") {
                        newValue = moment(newValue);
                    }
                    if (newValue !== oldValue)
                        utility_1.setValue(newValue, $scope, $ctrl, $attrs);
                });

You can check the exact change at this comit

@easyest
Copy link
Author

easyest commented Mar 8, 2021

With the additional watch shown above, there is a problem with validation - the value is not validated and is show invalid required even if set. The solution is to modify momentToValue method instead of adding a watch:

exports.momentToValue = function (momentObject, format) {
    if (typeof momentObject === "string") {
        momentObject = moment(momentObject);
    }
    if (!exports.isValidMoment(momentObject))
        return undefined;
    return !format ? momentObject.valueOf() : momentObject.format(format);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant