【问题标题】:angularui datepicker: TypeError: Cannot use 'in' operator to search for 'show-weeks' in undefinedangularui datepicker:TypeError:无法使用“in”运算符在未定义中搜索“show-weeks”
【发布时间】:2014-01-23 16:27:39
【问题描述】:

我想使用带有 angularui 的简单弹出日期选择器,例如 this。 当我将标记和 javascript 代码复制到我的应用程序时,我得到了这个:

TypeError: Cannot use 'in' operator to search for 'show-weeks' in undefined
    at link (http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.10.0.js:1247:43)
    at nodeLinkFn (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:6220:13)
    at compositeLinkFn (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:5630:15)
    at compositeLinkFn (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:5633:13)
    at compositeLinkFn (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:5633:13)
    at compositeLinkFn (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:5633:13)
    at nodeLinkFn (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:6214:24)
    at compositeLinkFn (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:5630:15)
    at compositeLinkFn (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:5633:13)
    at compositeLinkFn (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:5633:13) <input type="text" class="form-control ng-pristine ng-valid" datepicker-popup="{{format}}" ng-model="dt" is-open="opened" min="minDate" max="'2015-06-22'" datepicker-options="dateOptions" date-disabled="disabled(date, mode)" ng-required="true" close-text="Close"> angular.js:9413
(anonymous function) angular.js:9413
(anonymous function) angular.js:6832
nodeLinkFn angular.js:6223
compositeLinkFn angular.js:5630
compositeLinkFn angular.js:5633
compositeLinkFn angular.js:5633
compositeLinkFn angular.js:5633
nodeLinkFn angular.js:6214
compositeLinkFn angular.js:5630
compositeLinkFn angular.js:5633
compositeLinkFn angular.js:5633
publicLinkFn angular.js:5535
(anonymous function) angular.js:1303
Scope.$eval angular.js:11949
Scope.$apply angular.js:12049
(anonymous function) angular.js:1301
invoke angular.js:3704
doBootstrap angular.js:1299
bootstrap angular.js:1313
angularInit angular.js:1262
(anonymous function) angular.js:20549
trigger angular.js:2341
(anonymous function) angular.js:2612
forEach angular.js:309
eventHandler

我可以发布我的应用程序的代码,但基本上是这样的:

angular.module('appName', ['ui.bootstrap'])
  .controller("MyCtrl", function MyCtrl($scope, $http) { 
    $scope.today = function() {
      $scope.dt = new Date();
    };

  // ... the rest of the code EXACTLY like in the plnkr

  });

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

我也在使用与 plnkr 中相同的 angularjs/angularui/bootstrap CDN。为什么它发生在我的应用程序中,而在 plnkr 中它工作得很好?!

编辑:

在没有得到任何答案后,我决定在出现异常的第 1247 行附近更改 angularui 的源代码。我不是世界上最好的 javascript 程序员,所以在使用运算符 in 之前,我只是确保 datepickerOptions 是一个对象:

if (!((typeof(datepickerOptions) === "object")) || !('show-weeks' in datepickerOptions)) {
  scope.showWeeks = datepickerConfig.showWeeks;
} else {
  scope.showWeeks = datepickerOptions['show-weeks'];
}

不过,我仍在寻找真正的解决方案。

【问题讨论】:

  • 在你确定你知道它在做什么之前,我会避免编辑源代码。也许那时还没有。 ;)
  • @blurd +1 将其作为答案发布。如果 Angular 代码真的有问题,为什么只为自己修复呢?与 Angular 分享它,如果它确实是一个问题,那么他们会为每个人解决它。

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


【解决方案1】:

删除datepicker-options="dateOptions"

说明

您可以在 Popup Settings 下的the Documentation 中看到

datepicker 的选项可以使用 datepicker-options 属性作为 JSON 传递。

您复制的示例在指令中使用了datepicker-options="dateOptions"。该错误告诉您 Angular 正在尝试查找在 dateOptions 中设置的 show-weeks 值。问题是,dateOptions 没有在 html 中定义。

查看示例中的 JavaScript,您会看到缺少的变量是在控制器的作用域中定义的。

$scope.dateOptions = {
  'year-format': "'yy'",
  'starting-day': 1
};

【讨论】:

  • 太棒了。你怎么回答这个问题?
猜你喜欢
  • 2021-04-16
  • 1970-01-01
  • 2016-11-20
  • 2021-11-03
  • 2015-10-12
  • 2022-01-18
  • 2020-04-08
  • 1970-01-01
  • 2016-03-03
相关资源
最近更新 更多