类型错误:未定义对象实例的模块模式 [英] TypeError: Undefined on module pattern of object instances

查看:47
本文介绍了类型错误:未定义对象实例的模块模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对这个问题的一种跟进:

<块引用>

如何在一个页面上至少有两个 ui-bootstrap 的日期选择器?

它以一种优雅的方式在同一视图/页面上处理多个角度引导日期选择器元素的实例做得很好.至少它看起来......我收到以下错误.

<块引用>

TypeError: 无法设置未定义的属性BeginDate"

当我单击以尝试打开日期对话框时会发生这种情况.它发生在下面的行中,您可以在下面的代码示例中看到.实例对象未定义.

method.instances[instance] = true;

据我所知,我的代码和另一个问题中的示例几乎相同.

示例控制器代码:

$scope.datePicker = (function() {var 方法 = {};var 实例 = [];method.open = function ($event, instance) {$event.preventDefault();$event.stopPropagation();method.instances[instance] = true;};method.minDate = new Date();method.maxDate = new Date(2023, 12, 24);方法.选项 = {formatYear: "yyyy",起始日:1};method.format = "dd-MM-yyyy";返回方法;}());

示例元素代码:

<label for="beginDate" class="col-md-5">开始日期</label><input type="text" name="beginDate" ng-model="codeCamp.BeginDate" datepicker-popup="{{datePicker.format}}" min-date="datePicker.minDate" max-date="datePicker.maxDate" datepicker-options="datePicker.options" is-open="datePicker.instances['BeginDate']" ng-required="true" close-text="Close" class="form-control" required/><span class="input-group-btn"><button type="button" class="btn btn-default" ng-click="datePicker.open($event, 'BeginDate')"><i class="glyphicon glyphicon-calendar"></i></span>

<div class="form-group"><label for="endDate" class="col-md-5">结束日期</label><input type="text" name="endDate" ng-model="codeCamp.EndDate" datepicker-popup="{{datePicker.format}}" min-date="datePicker.minDate" max-date="datePicker.maxDate" datepicker-options="datePicker.options" is-open="datePicker.instances['EndDate']" ng-required="true" close-text="Close" class="form-control" required/><span class="input-group-btn"><button type="button" class="btn btn-default" ng-click="datePicker.open($event, 'EndDate')"><i class="glyphicon glyphicon-calendar"></i></span>

解决方案

您是否尝试过在方法对象中创建实例数组?

取而代之的是:

var method = {};var 实例 = [];

使用这个:

var method = {};method.instances = [];

告诉我!

This is kind of a follow-up to this question:

How to have at least two datepickers of ui-bootstrap on a single page?

It does a great job of having an elegant way to handle multiple instances of angular bootstrap datepicker elements on the same view/page. At least it appears to... I get the following error.

TypeError: Cannot set property 'BeginDate' of undefined

This occurs when I click to attempt to open the date dialog. It happens on the line below, which you can see in the code sample below that. The instances object is undefined.

method.instances[instance] = true;

From what I can tell, my code and the example in the other question are nearly identical.

Example controller code:

$scope.datePicker = (function() {

    var method = {};
    var instances = [];

    method.open = function ($event, instance) {
        $event.preventDefault();
        $event.stopPropagation();

        method.instances[instance] = true;
    };

    method.minDate = new Date();

    method.maxDate = new Date(2023, 12, 24);

    method.options = {
        formatYear: "yyyy",
        startingDay: 1
    };

    method.format = "dd-MM-yyyy";

    return method;
}());

Example element code:

<div class="form-group">
    <label for="beginDate" class="col-md-5">Begin Date</label>
    <input type="text" name="beginDate" ng-model="codeCamp.BeginDate" datepicker-popup="{{datePicker.format}}" min-date="datePicker.minDate" max-date="datePicker.maxDate" datepicker-options="datePicker.options" is-open="datePicker.instances['BeginDate']" ng-required="true" close-text="Close" class="form-control" required />
    <span class="input-group-btn">
        <button type="button" class="btn btn-default" ng-click="datePicker.open($event, 'BeginDate')"><i class="glyphicon glyphicon-calendar"></i></button>
    </span>
</div>
<div class="form-group">
    <label for="endDate" class="col-md-5">End Date</label>
    <input type="text" name="endDate" ng-model="codeCamp.EndDate" datepicker-popup="{{datePicker.format}}" min-date="datePicker.minDate" max-date="datePicker.maxDate" datepicker-options="datePicker.options" is-open="datePicker.instances['EndDate']" ng-required="true" close-text="Close" class="form-control" required />
    <span class="input-group-btn">
        <button type="button" class="btn btn-default" ng-click="datePicker.open($event, 'EndDate')"><i class="glyphicon glyphicon-calendar"></i></button>
    </span>
</div>

解决方案

Have you tried to create the instances array in the method object?

Instead of this:

var method = {};
var instances = [];

Use this:

var method = {};
method.instances = [];

Let me know!

这篇关于类型错误:未定义对象实例的模块模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆