无效的“;ng模型”;值

Invalid "ng-model" value with Bootstrap UI datepicker

本文关键字:模型 ng 无效      更新时间:2023-09-26

我正试图在AngularJS应用程序中实现一个简单的日期选择器指令。按照UI引导文档,我每次都会得到这个错误:

Datepicker指令:"ng模型"值必须是Date对象,数字自1970年1月1日起的毫秒数或表示RFC2822的字符串或ISO 8601日期。

我在这里和这里发现了一些其他相关的问题,但它们都没有帮助。

我尝试访问$parent和$parent$这里建议使用parent,这样可以消除错误,但不会更新模型。

这是一个Plunker重现问题:

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

<!DOCTYPE html>
<html>
  <head>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.1/angular.min.js"></script>
      <script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.13.0.js"></script>
 </head>
  <body ng-app="myApp" ng-controller="AppControl as appCtrl">
    <label>{{dt | date:'fullDate'}}</label>
    <input type="text" 
             class="form-control" 
             datepicker-popup="MMMM d, yyyy 'at' h:mm a" 
             ng-model="dt" 
             is-open="opened"
             datepicker-options="dateOptions"
             ng-required="true" 
             close-text="Close" />
    <span class="input-group-btn">
        <button type="button" class="btn btn-default" ng-click="open($event)">Pick</button>
    </span>
    <script>
      angular.module("myApp", ['ui.bootstrap']).controller("AppControl", ["$scope", function($scope) {
        $scope.opened = false;
        $scope.dateOptions = {};
        $scope.dt = new Date();
        $scope.open = function($event) {
            $event.preventDefault();
            $event.stopPropagation();
            $scope.opened = true;
        };
      }]);
    </script>
  </body>
</html>

问题在于您在属性"datepicker popup"中提供的格式。

使用属性值作为"longDate",如下所示:

datepicker-popup="longDate"

按如下方式使用:

<input type="text" 
             class="form-control" 
             datepicker-popup="fullDate" 
             ng-model="dt" 
             is-open="opened"
             datepicker-options="dateOptions"
             ng-required="true" 
             close-text="Close" />

我更新了plnkr。