显示当前日期AngularJS

时间:2016-06-29 12:49:44

标签: javascript angularjs datepicker

当用户导航到我的页面以查看我的html中显示的当前日期作为默认值时,我希望这样。

这是日期选择器选择的html:

        <div class="row">
    <div class="col-lg-12">
        <div class="panel panel-primary">
            <div class="panel-heading">
                {{card}}
            </div>
            <div class="panel-body">
                <div class="row">
                    <div class="col-lg-12">
                        <p>{{DatePickerCtrl.chosenStartDate | date : 'longDate'}} - {{DatePickerCtrl.chosenEndDate | date : 'longDate'}}</p>
                    </div>
                </div>
                <ng-include></ng-include>
            </div>
            <a href ng-click="footerLinkClicked()">
                <div class="panel-footer">
                    <span class="pull-left">Select Date</span>
                    <span class="pull-right"><i class="fa fa-arrow-circle-right"></i></span>
                    <div class="clearfix"></div>
                </div>
            </a>
        </div>
    </div>
</div>

这是我的日期选择器对应的控制器:

    angular.module('App').directive('datePicker', function($mdDialog, $rootScope, $filter) {
    return {
        restrict: 'E',
        templateUrl: 'components/date-picker-select/date-picker-select.html',
        scope: {
            card: '='
        },
        controller: function($scope) {
            var self = this;
            $rootScope.$on("dateWasChosen", function() {
                self.chosenStartDate = JSON.parse(localStorage.getItem('chosenStartDate'));
                self.chosenEndDate = JSON.parse(localStorage.getItem('chosenEndDate'));
            });
            $scope.footerLinkClicked = function() {
                $mdDialog.show({
                    controller: datePickerController,
                    templateUrl: 'components/date-picker-select/date-picker-modal.html',
                    parent: angular.element(document.body),
                    clickOutsideToClose: true
                }).then(function(answer) {
                    console.log("Fff");
                }, function() {});
            }
        },

        controllerAs: 'DatePickerCtrl'
    }
})
function datePickerController($scope, $mdDialog, $rootScope, datePickerFactory, $filter) {
    console.log("suntem in date picker sel");
    $scope.startDateL = new Date();
    $scope.endDateL = new Date();
    $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();
    }
}

我希望在我进入页面时显示今天的日期。

提前致谢!

2 个答案:

答案 0 :(得分:1)

首先在控制器中创建一个日期对象

function Ctrl($scope){
   $scope.date = new Date();
}

然后在你的模板中执行以下操作:

<div ng-app ng-controller="Ctrl">
    {{date | date:'yyyy-MM-dd'}}
</div>

function Ctrl($scope){
   $scope.date = new Date();
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-controller="Ctrl">
     {{date | date:'yyyy-MM-dd'}}
</div>

并按照您想要的方式对其进行格式化。

答案 1 :(得分:0)

您只需将Date object分配给您要评估此表达式的范围。