【问题标题】:AngularUI Datepicker resets time on the ngModelAngularUI Datepicker 重置 ngModel 上的时间
【发布时间】:2012-11-03 00:39:14
【问题描述】:

我已经连接了 AngularUI Datepicker,它工作得很好,除了当您在日历上选择不同的日期时,它会重置通过 ngModel 属性附加到它的 ISO 日期的时间部分。如何设置它以保持时间?

【问题讨论】:

    标签: coffeescript angularjs jquery-ui-datepicker


    【解决方案1】:

    我通过在指令中充实它来解决我的问题。

    input(ng-model="event.date_started", j-datepicker)
    div(ng-model="event.date_started", j-datepicker)
    

    .directive 'jDatepicker', ->
        require: 'ngModel'
        link: (scope, elem, attrs, ngModel) ->
            # check if elem is an input so when the input
            # gains focus it doesn't spawn another calender
            if not elem.is('input')
                elem.datepicker
                    onSelect: (dateText) ->
                        # get the original ISO date and new date
                        oldDate = new Date ngModel.$modelValue
                        newDate = new Date dateText
    
                        # set the new month and day onto the original ISO date
                        oldDate.setUTCMonth newDate.getUTCMonth()
                        oldDate.setUTCDate newDate.getUTCDate()
    
                        scope.$apply () ->
                            # update the model with the new info
                            ngModel.$setViewValue oldDate.toISOString()
    
            ngModel.$render = () ->
                date = new Date ngModel.$viewValue
    
                # set the default highlighted date on the calender
                elem.datepicker 'setDate', date
    
                # this renders the date value on the 
                # input element in the format I want
                elem.val date.toDateString()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-02
      • 2016-06-01
      • 1970-01-01
      • 2013-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多