AngularJS 1.3 - 日期选择器的初始格式不正确 [英] AngularJS 1.3 - Datepicker initial format is incorrect

查看:103
本文介绍了AngularJS 1.3 - 日期选择器的初始格式不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用AngularJS 1.3昨天和一个问题,我已经发现的是,在日期选择初始日期不在定义的格式。日期的格式是正确的之后的选择一个日期。

最初的格式与AngularJS 1.2.10正确的,因为可以在例如通过注释掉脚本标签中可以看出。

如何设置初始格式是正确的?

Plunker例如

HTML

 <!DOCTYPE HTML>
< HTML NG-应用=plunker>
  < HEAD>
    &LT;脚本的src =// ajax.googleapis.com/ajax/libs/angularjs/1.3.0-rc.0/angular.min.js\"></script>
    &LT;! - &LT;脚本的src =// ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.js\"></script> - &GT;
    &LT;脚本的src =// angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.11.0.js\"></script>
    &所述; SCRIPT SRC =example.js&GT;&下; /脚本&GT;
    &LT;链接HREF =// netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css的rel =stylesheet属性&GT;
  &LT; /头&GT;
  &LT;身体GT;  &LT; BR&GT;
  &LT; BR&GT;
  &LT; D​​IV NG控制器=DatepickerDemoCtrl&GT;
    &LT; D​​IV CLASS =行&GT;
        &LT; D​​IV CLASS =COL-MD-6&GT;
            &LT; p =类输入组&GT;
              &LT;输入类型=文本级=表格控日期选择器,弹出={{格式}}NG模型=DT是开=打开闽日期=MINDATE最大更新='2015年6月22日日期选择器选项=dateOptions日期禁用=禁用(日期模式)NG-所需=真密文=关闭/&GT;
              &LT;跨度类=输入组BTN&GT;
                &LT;按钮式=按钮级=BTN BTN-默认的NG点击=打开($事件)&GT;&LT; I类=glyphicon glyphicon日历&GT;&LT; / I&GT;&LT; /按钮&GT;
              &LT; / SPAN&GT;
            &所述; / P&GT;
        &LT; / DIV&GT;
    &LT; / DIV&GT;
  &LT; / DIV&GT;
  &LT; /身体GT;
&LT; / HTML&GT;

JS

  VAR应用= angular.module('plunker',['ui.bootstrap']);
app.controller(DatepickerDemoCtrl功能($范围){
  $ scope.format ='DD-MMMM-YYYY';  $ scope.today =功能(){
    $ scope.dt =新的日期();
  };
  $ scope.today();  $ scope.open =功能($事件){
    。$事件preventDefault();
    $ event.stopPropagation();    $ scope.opened = TRUE;
  };
});


解决方案

不知道在1.3究竟是什么改变,但有一个的的错误报告。

下面是直到修复bug修复该问题的初始值一个下拉指令。这里是一个演示 Plunker

  angular.module('对myApp')。指令(datepickerPopup',函数(dateFilter,datepickerPopupConfig){
    返回{
        限制:'A',
        优先权:1,
        要求:'ngModel',
        链接:功能(范围,元素,属性ngModel){
            VAR DATEFORMAT = attr.datepickerPopup || datepickerPopupConfig.datepickerPopup;
            ngModel。$ formatters.push(函数(值){
                返回dateFilter(值,日期格式);
            });
        }
    };
});

I started using AngularJS 1.3 yesterday and one problem I have found is that the initial date in the Datepicker is not in the defined format. The format of the date is correct after choosing a date.

The initial format is correct with AngularJS 1.2.10 as can be seen in the example by commenting out the script tag.

How can I set the initial format to be correct?

Plunker example

HTML

<!doctype html>
<html ng-app="plunker">
  <head>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-rc.0/angular.min.js"></script>
    <!-- <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.js"></script> -->
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.11.0.js"></script>
    <script src="example.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>

  <br>
  <br>
  <div ng-controller="DatepickerDemoCtrl">
    <div class="row">
        <div class="col-md-6">
            <p class="input-group">
              <input type="text" class="form-control" datepicker-popup="{{format}}" ng-model="dt" is-open="opened" min-date="minDate" max-date="'2015-06-22'" datepicker-options="dateOptions" date-disabled="disabled(date, mode)" ng-required="true" close-text="Close" />
              <span class="input-group-btn">
                <button type="button" class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
              </span>
            </p>
        </div>
    </div>
  </div>
  </body>
</html>

JS

var app = angular.module('plunker', ['ui.bootstrap']);
app.controller("DatepickerDemoCtrl", function ($scope) {
  $scope.format = 'dd-MMMM-yyyy';

  $scope.today = function() {
    $scope.dt = new Date();
  };
  $scope.today();

  $scope.open = function($event) {
    $event.preventDefault();
    $event.stopPropagation();

    $scope.opened = true;
  };
});

解决方案

Not sure exactly what changed in 1.3, but there is a bug report for this.

Here's a drop-in directive for the initial value that fixes the problem until the bug is fixed. And here is a demo Plunker.

angular.module('myApp').directive('datepickerPopup', function (dateFilter, datepickerPopupConfig) {
    return {
        restrict: 'A',
        priority: 1,
        require: 'ngModel',
        link: function(scope, element, attr, ngModel) {
            var dateFormat = attr.datepickerPopup || datepickerPopupConfig.datepickerPopup;
            ngModel.$formatters.push(function (value) {
                return dateFilter(value, dateFormat);
            });
        }
    };
});

这篇关于AngularJS 1.3 - 日期选择器的初始格式不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆