未捕获的TypeError:undefined不是函数knockout js

时间:2015-01-07 04:38:50

标签: javascript jquery knockout.js

我试图将某些数据添加到数组中,如下所示:

这是我的代码: -

create.html上

                                     字段名称:                                                                            显示名称:                                                                            添加字段                              
    </fieldset>
    <table class="table" style="margin:20px 0px">
        <thead>
            <tr>
                <th>Field Name</th>
                <th>Field Display Name</th>

            </tr>
        </thead>
        <tbody data-bind="foreach: fields">
            <tr>
                <td data-bind="text: fieldName"></td>
                <td data-bind="text: fieldDisplayName"></td>
                <td>
                    <div style="height:20px; width:20px; border:1px solid #000000; padding: 5px" data-bind="style: { backgroundColor : color }"></div>
                </td>
                <td><button class="btn btn-danger btn-circle" data-bind="click: remove"><i class="glyphicon glyphicon-remove"></i></button></td>
            </tr>
        </tbody>
    </table>

Table.js

  var ViewModel = function () {

    var self = this;
    self.fields = ko.observableArray();
    self.fieldName = ko.observable();
    self.fieldDisplayName = ko.observable();
    self.isRangeError = ko.observable(false);

    var Field = function (fieldName, fieldDisplayName) {

        this.fieldName = fieldName;
        this.fieldDisplayName = fieldDisplayName;
        this.remove = function () {
            self.fields.remove(this);
        }
    }

    self.addFields = function () {
        var tr = self.fields();
        for (var i = 0; i < tr.length; i++) {
            if (self.fieldName == tr[i].fieldName && self.fieldDisplayName == tr[i].fieldDisplayName) {
                self.isFieldError(true);
                return;
            }

        }
        self.isFieldError(false);
        self.fields.push(new Field(self.fieldName(), self.fieldDisplayName()));

    }

我得到一个像这样的错误: -

Uncaught TypeError: undefined is not a functionTableChart.js:41 self.addFieldsknockout-3.2.0.debug.js:3713 (anonymous function)jquery-1.10.2.js:5109 jQuery.event.dispatchjquery-1.10.2.js:4780 elemData.handle

我该如何解决这个问题?我是淘汰赛和javascript的新手。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

您声明的可观察对象存在问题。

self.isRangeError = ko.observable(false);

应该是:

 self.isFieldError = ko.observable(false);

颜色未在您的模型上定义,但在表格中使用。