model属性未定义

时间:2014-06-01 15:56:33

标签: javascript knockout.js

我正在制作一个简单的淘汰应用,但我收到错误"错误:无法解析绑定。消息:ReferenceError:未定义gpaBasedScolarship;绑定值:可见:gpaBasedScolarship"

我的代码是:

$(document).ready(function () {
    $.get("/api/Values", function (data) {
        console.log(data);
            function studentModel(name, number, age, gpa) {
                var self = this;
                self.studentName = name;
                self.studentNumber = number;
                self.age = age;
                self.gpa = ko.observable(gpa);
                self.gpaBasedScolarship = ko.computed(function () {
                    if (gpa > 3)
                        return true;
                    else
                        return false;   
                });
            }
            function studentViewModel() {
                var self = this;
                //editable data
                self.students = ko.observableArray(data);
            }
            ko.applyBindings(new studentViewModel());
    });
});

是什么给出了?

PS:我的HTML是......

<div id="body">
    <div data-bind="foreach:students">
        <h1>Student</h1>
    <div><input type="text" data-bind="value:studentName"/></div>
    <div><input type="text" data-bind="value:studentNumber"/></div>
    <div><input type="text" data-bind="value:age"/></div>
    <div>
        <p><input type="text" data-bind="value:gpa"/></p>
        <p data-bind="visible: gpaBasedScolarship"><input type="text" data-bind=""/></p>
    </div>
        </div>
</div>

1 个答案:

答案 0 :(得分:2)

您还没有告诉淘汰赛将data映射到studentModel

var mappedData = ko.utils.arrayMap(data, function(item) {
    return new studentModel(...);
});
self.students = ko.obserbableArray(mappedData);