在datepicker AngularJS上设置当前日期

时间:2016-06-17 07:09:28

标签: angularjs datepicker

因此,当我点击m md-card上的按钮选择日期时,会打开一个模态。这是它的代码:

      <!-- Modal -->
 <md-dialog aria-label="Date Picker">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal"
                aria-hidden="true">&times;</button>
            <h4 class="modal-title" id="myModalLabel">{{modalTitle}}</h4>
        </div>
        <!-- /.modal-header -->
        <div class="modal-body">
            <md-input-container flex> <input type="date"
                ng-model="startDateL"> </md-input-container>
            <md-input-container flex> <label>{{}}</label> <input
                type="date" ng-model="endDateL"> </md-input-container>
            <!-- /.modal-search-box -->

            <md-button ng-click="setDate()" class="md-primary">Update
            Date</md-button>
        </div>
        <!-- /.modal-body -->
    </div>
    <!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
</md-dialog>
<!-- /.modal -->

这是我的控制器选择日期的代码

function datePickerController($scope, $mdDialog, $rootScope, datePickerFactory, $filter){
    console.log("suntem in date picker sel");
    $scope.startDateL = new Date($rootScope.startDate);
    $scope.endDateL = new Date($rootScope.endDate);

    $scope.close = function() {
        $mdDialog.hide();
        console.log("closing");
    }



    $scope.setDate = function(startDate, endDate) {
        $rootScope.startDate = $scope.startDateL;
        $rootScope.endDate = $scope.endDateL;

        var stDate = $filter('date')($rootScope.startDate,'yyyy-MM-dd');
        var enDate = $filter('date')($rootScope.endDate,'yyyy-MM-dd');

        console.log(stDate);
        console.log(enDate);

        datePickerFactory.save({sDate: stDate, eDate: enDate}, function(){
        })

        localStorage.setItem('chosenStartDate', JSON.stringify($rootScope.startDate));
        localStorage.setItem('chosenEndDate', JSON.stringify($rootScope.endDate));
        $rootScope.$broadcast("dateWasChosen");
        $mdDialog.hide();

    }

}

现在,我想要做的是在我的模态中设置我日期字段的当前日期,这样当我打开我的模态时,我有一个默认日期。那里有一个可以设置当前日期的函数吗?

1 个答案:

答案 0 :(得分:0)

您必须在控制器中创建日期对象。

$scope.startDateL = new Date();
$scope.endDateL  = new Date();