简体   繁体   中英

How to format ng-model date inside ng-repeat?

I would like to display date in a format of 'yyyy-MM-dd' while saving the value in event.StartDate

If I remove the ng-model, it can show the correct date, which is '2017-01-26 12:00', but the value cannot be saved into event.StartDate.

However, if I add back the ng-model, the date format does not work and '/Date(1485403200000)/' is shown.

Here is my code.

<div ng-repeat="event in Events">    
    <input name="eventStartDate" type="text" ng-model="event.StartDate" value="{{event.StartDate.replace('/Date(','').replace(')/','') | date:'yyyy-MM-dd HH:mm'}}" />
</div>                          

You need to apply the date filter to ng-model and you don't need the value attribute. Try with:

<input name="eventStartDate" ng-model="event.StartDate | date: 'yyyy-MM-dd'" type="text"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM