【问题标题】:Angular UI date picker validationAngular UI 日期选择器验证
【发布时间】:2017-05-01 01:50:16
【问题描述】:

In this plunk 我有一个 Angular UI 日期选择器和一条仅在日期有效时才显示的消息。问题是消息总是显示,即使form.$valid 为真。请注意,如果您输入一个有效或无效的日期,然后单击Validate,则会正确显示该日期分别有效或无效的警报。

为什么一直显示验证消息,即使form.$valid 为真?

Javascript

var app = angular.module('ui.bootstrap.demo', ['ui.bootstrap']);

app.controller('ctl', function ($scope) {


    $scope.dtFrom = new Date ();

    $scope.config1 = {};
    $scope.config1.opened = false;
    $scope.open1 = function(event){
         event.preventDefault();
         event.stopPropagation();
         $scope.config1.opened = true;
    };

    $scope.validate = function(form){
        if (!form.$valid) {
           alert("Date invalid");
        }
        else {
           alert("Date valid");
        }
    }

});

HTML

<div ng-controller="ctl">
  <form name="form1" ng-submit="validate(form1)" novalidate>
      <p class="input-group" name="dtFrom" style="width:160px;margin-bottom:0px;">
          <input type="text" class="form-control" ng-model="dtFrom" 
               is-open="config1.opened"  uib-datepicker-popup="MM-dd-yyyy" 
               close-text="Close" />
           <span class="input-group-btn">
               <button type="button" class="btn btn-default" ng-click="open1($event)">
                  <i class="glyphicon glyphicon-calendar"></i>
               </button>
           </span>
      </p>
      <span ng-show="!form1.dtFrom.$valid">Invalid Date</span>
      <br/>
      <button type="submit">Validate</button>
  </form>
</div>

【问题讨论】:

    标签: angularjs angular-ui-bootstrap angular-ui


    【解决方案1】:

    您应该为输入创建名称

     <input type="text" class="form-control" ng-model="dtFrom" 
                       is-open="config1.opened"  uib-datepicker-popup="MM-dd-yyyy" 
                       close-text="Close" name="date" />
          <span ng-show="!form1.date.$valid">Invalid Date</span>
    

    这里是 plnkr

    http://plnkr.co/edit/gc0Oefhok6Gjim66UG0K?p=preview

    【讨论】:

    猜你喜欢
    • 2016-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-28
    相关资源
    最近更新 更多