【发布时间】: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