I have an HTML5 date input and I would like its value to be set to the value of the date property in my model by default. I'm not too fussy about formatting since Chrome seems to decide that for me anyway based on my locale, but ideally the format would be consistently dd/MM/yyyy
.
Fiddle
This is how I set up my input:
<input type="date"
ng-model="date"
value="{{ date | date: 'yyyy-MM-dd' }}" />
This works fine on Chrome, and I see the following by default:
(I still don't quite understand why the value had to be given in yyyy-MM-dd
, if Chrome still formats it based on my locale, but that's a different question.)
My issue is with Firefox not showing the date's value in the way I've specified. I think this has to do with binding the input to the date
model, because I can specify pretty much any string in the value
attribute, and I will still see the long date string in the input by default:
If I remove ng-model="date"
from the input tag, Firefox nicely displays any value I give it. I didn't think the model that an input was bound to actually had any effect on its default value?
I understand the date input isn't supported universally, but seeing as it's supposed to fall back on a simple text input, I don't see why its value won't simply be 2013-08-05
, as specified by angular's date filter.
So, how do I get Firefox to accept my formatted value in the date input?
NOTE After the edits have been done by the user, I will of course perform validation and convert each date input value into a proper Date
object. Not sure if this is relevant to the question, but putting it out there just in case, because the input formats would obviously need to be consistent for the date conversion to work the same in all browsers. Problematic, of course, with Chrome deciding the input format for me...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…