Skip to content
This repository has been archived by the owner on May 28, 2019. It is now read-only.

Be more forgiving with empty strings in ng-model expressions #11

Open
steinerj opened this issue May 22, 2013 · 3 comments
Open

Be more forgiving with empty strings in ng-model expressions #11

steinerj opened this issue May 22, 2013 · 3 comments

Comments

@steinerj
Copy link

          // Update the date picker when the model changes
          controller.$render = function () {
              var date = controller.$viewValue;
            if ( angular.isDefined(date) && date !== null && !angular.isDate(date) ) {
              throw new Error('ng-Model value must be a Date object - currently it is a ' + typeof date + ' - use ui-date-format to convert it from a string');
            }
            element.datepicker("setDate", date);
          };

Apologies to start off with a code block. I've read that this part is intentionally strict in what it accepts (Date objects, null or undefined). However, when you define a ng-model, e.g. on an input, it defaults to empty string (""). In this case one has to define the value explicitly to null or undefined (in controller or via ng-init), otherwise it will throw the exception. Wouldn't it be reasonable to be more forgiving and treat emtpy strings like nulls in this place?

@petebacondarwin
Copy link
Member

Can you provide a plunker that demonstrates this issue?
The $render should only be called when you update the model value programmatically.

@JaKXz
Copy link

JaKXz commented Jan 9, 2015

I think I'm having a similar issue... my initial ng-model value is not shown in the text field, I fear because it comes back from my server in the form: 2015-01-09 12:00:00 AM

My input field looks like this:

<input ui-date ui-date-format="yyyy-MM-dd" name="effectiveDate" ng-model="squad.effectiveDate" />

I should also add that the date selected does not display in the format I've specified.

@alexanderchan
Copy link
Contributor

@JaKXz ui-date doesn't currently handle multiple formats that well unless custom parsers are defined. If it is really coming from the server in a different format, for now the easiest way would be to convert it to the date format you are expecting before using it.

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

No branches or pull requests

4 participants