我一直在使用淘汰赛一段时间,但在编写一些代码示例时,这个让我难过。代码完全按照我的预期工作,点击按钮切换可见,但我仍然收到错误:
JsFiddle:http://jsfiddle.net/JasonMore/hCdF8/1/
Uncaught Error: Unable to parse bindings.
Message: ReferenceError: answerClick is not defined;
Bindings value: click: answerClick
这
<ul data-bind="foreach: answers">
<li>
<a class="hiddenButton" href="#" data-bind="click: answerClick" />
<div class="answerNumber" data-bind="visible: showAnswerNumber">
<h2 data-bind="text: answerNumber" />
</div>
<div class="answer" data-bind="visible: showAnswerText">
<p data-bind="text:text" />
<p data-bind="text: points" />
</div>
</li>
</ul>
<script type="text/javascript">
var answerViewModel = function () {
var self = this;
//clicks
self.answerClick = function () {
self.showAnswerNumber(!self.showAnswerNumber());
};
//observables
self.answerNumber = ko.observable();
self.text = ko.observable();
self.points = ko.observable();
self.showAnswerNumber = ko.observable(true);
//computed
self.showAnswerText = ko.computed(function () {
return !self.showAnswerNumber();
});
};
var roundViewModel = function () {
var self = this;
self.answers = ko.observableArray();
};
var answer1 = new answerViewModel();
answer1.text = "foo answer";
answer1.answerNumber = 1;
answer1.points = 50;
var vm = new roundViewModel();
vm.answers.push(answer1);
ko.applyBindings(vm);
</script>
答案 0 :(得分:3)
因为您要关闭锚标记:<a />
,所以它会以奇怪的方式呈现,其中额外的锚标记位于foreach
之外,而不是answerClick
的上下文中是一个有效的功能。
我不确定您要为链接使用哪些内容,但您需要有一个开始和结束标记,如:
<a class="hiddenButton" href="#" data-bind="click: answerClick">link</a>
不确定您是否打算将内容包装在该链接中,但这就是您的问题所在。