Bootstrap 3 DateTimePicker - 在'dp.change'之后绑定到角度模型不起作用

时间:2016-05-04 12:03:22

标签: javascript angularjs bootstrap-datetimepicker eonasdan-datetimepicker

我正在尝试使用Bootstrap 3 DateTimePicker中的eonasdan。一切正常,但我遇到了将输入中的实际日期绑定到角度ng模型的问题。当我使用选择器更改数据时,'dp.change'事件正在运行,日期在输入内部发生变化,但我必须按空格键或任何其他键来实现模型更新检测。

一些代码的和平:

带角度指令的

JS:

(function () {
    'use strict';

    var module = angular.module('feedApp');

    module.directive('datetimepicker', [
        '$timeout',
        function ($timeout) {
            return {
                require: '?ngModel',
                restrict: 'EA',
                scope: {
                    options: '@',
                    onChange: '&',
                    onClick: '&'
                },
                link: function ($scope, $element, $attrs, controller) {
                    $($element).on('dp.change', function () {
                        //alert("change");
                        $timeout(function () {
                            var dtp = $element.data('DateTimePicker');
                            controller.$setViewValue(dtp.date());
                            $scope.onChange();
                        });
                    });

                    $($element).on('click', function () {
                        $scope.onClick();
                    });

                    controller.$render = function () {
                        if (!!controller) {
                            if (controller.$viewValue === undefined) {
                                controller.$viewValue = null;
                            }
                            else if (!(controller.$viewValue instanceof moment)) {
                                controller.$viewValue = moment(controller.$viewValue);
                            }
                            $($element).data('DateTimePicker').date(controller.$viewValue);
                        }
                    };

                    $($element).datetimepicker($scope.$eval($attrs.options));
                }
            };
        }
    ]);
})();

HTML:

<div style="max-width: 250px">
    <div class="input-group input-group-sm date">
        <input type="text" class="form-control" options="{format:'DD.MM.YYYY HH:mm', locale:'pl'}" datetimepicker ng-model="feed.reminderDateTime" />
        <span class="input-group-addon">
            <span class="glyphicon glyphicon-calendar"></span>
        </span>
    </div>
</div>

知道怎么解决这个问题吗?我将不胜感激。

2 个答案:

答案 0 :(得分:0)

回来时有类似的问题。没有引用的源代码,但我记得它是这样的。

因为你的ng模型是ng-model="feed.reminderDateTime"

在您的控制器中,您应该执行此操作$scope.feed = {reminderDateTime:''};

然后您应该可以通过$scope.feed.reminderDateTime

访问它

让我知道这是怎么回事

答案 1 :(得分:0)

要求(&#39; datetimepicker&#39;);

Vue.directive('datetimepicker', {
    twoWay: true, //very important to bind the modeldata
    bind: function () {
        $(this.el).datetimepicker({
            format: 'YYYY/MM/DD',    //see moment for more valid date time formats
            useCurrent: false
        }).on('dp.change', function (value) {
            //todo: do the required changed to the value here 
        });
    },
    update: function (value) {
        $(this.el).val(value).trigger('dp.change');
    },
    unbind: function () {
        $(this.el).off().datetimepicker('destroy');
    }
});
相关问题