代码之家  ›  专栏  ›  技术社区  ›  Velocibear

Angular UI引导日期选择器与UI.Mask组合

  •  13
  • Velocibear  · 技术社区  · 10 年前

    我正在使用角度 UI bootstrap popup date-picker 构建一个指令,使我可以在需要时轻松添加日期选择器。

    当我将此与 uiMask Directive ,当我选择日期时,输入中的值会被打乱。

    这是我的html :

    <p class="input-group">
        <input type="text" class="form-control" 
               ui-mask="99/99/9999"
               ng-model="ngModel" 
               ng-model="order.date" 
               datepicker-popup="MM/dd/yyyy" 
               is-open="opened" 
               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>
    

    还有我的JS :

    /**
     * DATE PICKER
     */
    $scope.today = function () {
        $scope.dt = new Date();
    };
    $scope.today();
    
    $scope.clear = function () {
        $scope.dt = null;
    };
    
    // Disable weekend selection
    $scope.disabled = function (date, mode) {
        return (mode === 'day' && (date.getDay() === 0 || date.getDay() === 6));
    };
    
    $scope.toggleMin = function () {
        $scope.minDate = $scope.minDate ? null : new Date();
    };
    $scope.toggleMin();
    
    $scope.open = function ($event) {
        $event.preventDefault();
        $event.stopPropagation();
    
        $scope.opened = true;
    };
    
    $scope.dateOptions = {
        formatYear: 'yy',
        startingDay: 1
    };
    
    $scope.initDate = new Date('2016-15-20');
    $scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
    $scope.format = $scope.formats[0];
    

    我希望能够使用ui掩码功能,通过不必键入 / s.是否可以一起使用这些?

    3 回复  |  直到 10 年前
        1
  •  13
  •   Igor de Lorenzi    9 年前

    以下代码片段对我有用:

    .config(function ($provide) {
    
      $provide.decorator('datepickerPopupDirective', function ($delegate) {
        var directive = $delegate[0];
        var link = directive.link;
    
        directive.compile = function () {
          return function (scope, element, attrs) {
            link.apply(this, arguments);
            element.mask("99/99/9999");
          };
        };
    
        return $delegate;
      });
    
    });
    

    它只是用由 jquery.maskedinput.js 玩得高兴


    更新(2015年5月13日)

    一个双关语来说明它的作用: http://plnkr.co/edit/fTFNu9Mp2kX5X1D6AJOx?p=preview

        2
  •  3
  •   patlv23    8 年前

    看看这个例子中的案例4: https://plnkr.co/edit/0Vr5YRVREIT5EMu1m55z?p=preview

    基本添加:添加 模型视图值=“true” :

    <input name="date4" id="date4" type="text" class="form-control" uib-datepicker-popup="dd/MM/yyyy" 
        ui-mask="99/99/9999" placeholder="DD/MM/YYYY" ng-model="date4" ng-required="true" 
        model-view-value="true" is-open="popup4.opened" datepicker-options="dateOptions" 
        show-button-bar="false"/>
    <span class="input-group-btn">
        <button type="button" class="btn btn-default" ng-click="open4()"><i class="fa fa-calendar fa-1"></i></button>
    </span>
    
        3
  •  2
  •   Robin van Baalen    10 年前

    当我试图重现你的挑战时,我首先想到的是:

    new Date('2016-15-20');
    

    回报

    Invalid Date
    

    也许这是一个开始寻找解决方案的地方。那是什么日期?第15个月还是第20个月?不管怎样,这都行不通。提供有效日期 $scope.initDate 。也许你的意思是“2016-12-20”?

    您能否为您的情况创建一个小示例,以便我们测试可能的解决方案?你所说的“加扰输入”到底是什么?再次,请提供问题和预期结果的示例。