Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs date input allowing null input

Relatively new to Angularjs

I have read the date input documentation here: Angularjs Date Input It states the model (the data the input is bound to) must be a JavaScript date object.

However, in my application, I want to be able display a date input field that is initially empty (except perhaps for a placeholder prompt). If the user chooses not to enter a date, that is an acceptable circumstance.

My understanding is that a JavaScript date object always describes some date. So the date input, since it is bound to the model, will always display some date. And if the user overlooks or skips the date input, a date will still seem to have been provided. I don't want that to happen.

The only solution I have think of would be to use a text edit field that can be empty or contain a date. And then I would have to somehow parse out the user input. But it wouldn't have a nice date picker.

Any suggestions on how to solve this issue?

like image 311
kdtop Avatar asked Jul 25 '14 00:07

kdtop


2 Answers

Well, you can actually simply set the value to undefined.

$scope.value = undefined won't throw an error.

See working plunkr : http://plnkr.co/edit/5Nf9UeC5XApAAST1eRJX?p=preview (Edited from previous reply by OP.)

like image 190
Tete1805 Avatar answered Oct 01 '22 15:10

Tete1805


As mentioned by Tete1805, it is the correct solution.

Just wanted to share a case i had experienced in this regard. A case in which it was returning me a value like "0NaN-NaN-NaN" that's while using :

document.getElementById('date'+index).value;

It can be resolved by simply checking

if(document.getElementById('date'+index).value == "0NaN-NaN-NaN"){
//Error handling code here
}
like image 27
Kailas Avatar answered Oct 01 '22 16:10

Kailas