Uncaught TypeError: undefined is not a function knockout js

Uncaught TypeError: undefined is not a function 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 很陌生。感谢任何帮助。

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

self.isRangeError = ko.observable(false);

应该是:

 self.isFieldError = ko.observable(false);

此外,颜色未在您的模型上定义,但在 table 中使用。