TypeError:scope.viewDate.clone不是函数

时间:2018-01-21 00:33:41

标签: angularjs momentjs

我正在尝试使用angular和momentjs构建日期时间选择器。 我已经建立了我的指令来填充日期但是“得到错误”TypeError:scope.viewDate.clone不是一个函数“我不太确定我哪里出错了以及如何解决它。

angular.module('myApp')
.directive('datepicker', function () {
    return{
        restrict: 'EA',
        scope: true,
        require: 'ngModel',
        link: function (scope, elm, attr, ngModel) {

            scope.viewDate = moment;
            var selectedDate = null;

            function generateDays() {
                scope.days = [];

                var startOfSelectedDate = moment.isMoment(selectedDate) ? selectedDate.clone().startOf('day') : null;
                var startDate = scope.viewDate.clone().startOf('month').startOf('week').startOf('day');
                var endDate = scope.viewDate.endOf('month').endOf('week').endOf('day');

                while (startDate < endDate){
                    scope.days.push({
                        label: startDate.date(),
                        inMonth: startDate.month() === scope.viewDate.month() && startDate.year() === scope.viewDate.year(),
                        date: startDate.valueOf(),
                        selected: startDate.isSame(selectedDate)
                    });
                    startDate.add(1, 'day');
                }
            }
            generateDays();
        }
    }
});

1 个答案:

答案 0 :(得分:1)

您在定义viewDate时错过了执行该操作的时间。你的代码将是

scope.viewDate = moment();