在 Jquery 模板中找不到 knockout.js 和属性
我使用下面的代码收到“caseStudy 未定义”。我必须添加完整的前缀 app.viewModel.caseStudy.showFlawDetails 才能不出错。
app.viewModel.caseStudy = {};
app.viewModel.caseStudy.cases = ko.observableArray();
app.viewModel.caseStudy.selectedCaseId = ko.observable(0);
app.viewModel.caseStudy.selectedCase = ko.mapping.fromJS(caseModel);
app.viewModel.caseStudy.showFlawDetails = function (index) {
console.log(index);
};
ko.applyBindings(app.viewModel);
<div class="Flaws" data-bind='template: { name: "flawTemplate", data: caseStudy.selectedCase.Flaws }'>
</div>
<script id="flawTemplate" type="text/html">
{{each(index, value) $data}}
<div class="flaw">
<div class="Title" data-bind="click: caseStudy.showFlawDetails(index)"> ${value.Title} </div>
<div class="Items">
<div>Title: <input type="text" data-bind="value: value.Title" /></div>
<div>Check: <input type="text" data-bind="value: value.Check" /></div>
<div>Instructor: <input type="text" data-bind="value: value.Instructor" /></div>
<div>Keys: <input type="text" data-bind="value: value.Keys" /></div>
<div>Opponent Init: <input type="text" data-bind="value: value.OpponentInit" /></div>
<div>Opponent Justification: <input type="text" data-bind="value: value.OpponentJustif" /></div>
<div>Opponent Communication: <input type="text" data-bind="value: value.OpponentComm"/></div>
<div>Hint: <input type="text" data-bind="value: Hint"/></div>
<div>Opponent Incorrect Hint: <input type="text" data-bind="value: value.OpponentIncorrectHint"/></div>
<div>Prompt: <input type="text" data-bind="value: Prompt" /></div>
<div>PromptCompletion: <input type="text" data-bind="value: value.PromptCompletion"/></div>
<div>Opponent Incorrect Prompt: <input type="text" data-bind="value: value.OpponentIncorrectPrompt"/></div>
</div>
</div>
{{/each}}
</script>
I am getting "caseStudy is not defined" with the below code. I have to add the full prefix app.viewModel.caseStudy.showFlawDetails to not error.
app.viewModel.caseStudy = {};
app.viewModel.caseStudy.cases = ko.observableArray();
app.viewModel.caseStudy.selectedCaseId = ko.observable(0);
app.viewModel.caseStudy.selectedCase = ko.mapping.fromJS(caseModel);
app.viewModel.caseStudy.showFlawDetails = function (index) {
console.log(index);
};
ko.applyBindings(app.viewModel);
<div class="Flaws" data-bind='template: { name: "flawTemplate", data: caseStudy.selectedCase.Flaws }'>
</div>
<script id="flawTemplate" type="text/html">
{{each(index, value) $data}}
<div class="flaw">
<div class="Title" data-bind="click: caseStudy.showFlawDetails(index)"> ${value.Title} </div>
<div class="Items">
<div>Title: <input type="text" data-bind="value: value.Title" /></div>
<div>Check: <input type="text" data-bind="value: value.Check" /></div>
<div>Instructor: <input type="text" data-bind="value: value.Instructor" /></div>
<div>Keys: <input type="text" data-bind="value: value.Keys" /></div>
<div>Opponent Init: <input type="text" data-bind="value: value.OpponentInit" /></div>
<div>Opponent Justification: <input type="text" data-bind="value: value.OpponentJustif" /></div>
<div>Opponent Communication: <input type="text" data-bind="value: value.OpponentComm"/></div>
<div>Hint: <input type="text" data-bind="value: Hint"/></div>
<div>Opponent Incorrect Hint: <input type="text" data-bind="value: value.OpponentIncorrectHint"/></div>
<div>Prompt: <input type="text" data-bind="value: Prompt" /></div>
<div>PromptCompletion: <input type="text" data-bind="value: value.PromptCompletion"/></div>
<div>Opponent Incorrect Prompt: <input type="text" data-bind="value: value.OpponentIncorrectPrompt"/></div>
</div>
</div>
{{/each}}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的
flawTemplate
内部,范围是caseStudy.selectedCase.Flaws
,因此当您放置caseStudy.showFlawDetails
时,不会发现它是缺陷
或全球性的。因此,如果
app
具有全局范围(似乎是这样,因为它适合您),您可以使用app.viewModel.caseStudy.showFlawDetails
引用它。否则,一个好的选择是通过 templateOptions 传递函数。因此,您可以这样做:
然后,您将使用
$item.showFlawDetails
访问它。单击(和事件)绑定还期望您向其传递对函数的引用。在您的情况下,您将向其传递执行该函数的结果。在这里进一步回答:knockout.js 调用 click 即使当jquery 模板已渲染
Inside of your
flawTemplate
the scope iscaseStudy.selectedCase.Flaws
, so when you putcaseStudy.showFlawDetails
, it is not found as a property ofFlaws
or globally.So, you can either reference it with
app.viewModel.caseStudy.showFlawDetails
, ifapp
has global scope (which it seems to since it works for you).Otherwise, a good option is to pass the function in via templateOptions. So, you would do:
Then, you would access it using
$item.showFlawDetails
The click (and event) bindings also expect that you pass it a reference to a function. In your case you are passing it the result of executing the function. Answered it further here: knockout.js calling click even when jquery template is rendered