我是一个 KO 新手,我正在做一个小型演示项目来开始使用 KO。这是一个小表格,有问题和下拉选择答案,用户可以从中选择。
HTML 片段是
<div data-bind="foreach: $root.bookArr1" >
<p> Which book says <span data-bind="text: $data.bookQoute"></span>.</p>
<p><select data-bind="options: $root.bookArr, value: answer, optionsText: 'bookName', uniqueName: true"></select></p>
</div>
视图模型如下(仅片段)
this.firstName = ko.observable();
this.lastName = ko.observable();
this.answer = ko.observable();
this.bookQoutes = ([
{ bookQoute: "Curiouser and curiouser!", bookName: "Alice in wonderland" },
{ bookQoute: "Standard (iceream)", bookName: "PQR" },
{ bookQoute: "Standard (honey)", bookName: "LMY" },
{ bookQoute: "Standard (lemon)", bookName: "JND" }
]);
this.bookArr1 = ko.observableArray( this.bookQoutes );
this.bookArr = ko.observableArray( this.bookQoutes );
我的问题是,当我从我的 html 中删除 value : answer 时,它运行良好并显示四行及其下方的下拉菜单。
但是当我添加 value: answer 时,我得到以下异常
Unable to parse bindings. Message: ReferenceError: answer is not defined;
Bindings value: options: $root.bookArr, value: answer, optionsText: 'bookName', uniqueName: true
谁能指出我在这里做错了什么。我到处查找,但似乎无法找到解决此问题的方法。
谢谢蒂塔什