gpt4 book ai didi

ajax - Knockout.js 不从动态 ajax 调用更新 View

转载 作者:行者123 更新时间:2023-12-04 06:44:16 26 4
gpt4 key购买 nike

我试图让 knockout.js 在 ajax 调用后更新我的 View ,但没有成功。我不断收到以下错误。它在 app.interactive.updateBindings 处出错。 parseBindingAttribute viewModel 参数未定义。

错误:

Unable to parse binding attribute. Message:
ReferenceError: interactive is not defined;
Attribute value: template: { name: 'answerTmpl', foreach: interactive.answers }

动态 HTML:

<div id="questions">
<div id="answers" data-bind="template: { name: 'answerTmpl', foreach: interactive.answers }">
</div>
</div>

<script type="text/javascript">

app.viewModel.interactive.answers(@Html.Raw(@Model.Answers.ToJson()));
app.interactive.updateBindings(document.getElementById('answers'));

</script>
<script id="answerTmpl" type="text/html">
<div>
<span></span> <input type="checkbox" name="Answer" />
</div>
</script>

我的 .js 文件:

(function (app, $, undefined) {

app.viewModel = {};
app.interactive = {};
app.interactive.callback = function () { };

app.viewModel.interactive = {
content: ko.observable('test'),
answers: ko.observableArray()
};

app.interactive.init = function () {

ko.applyBindings(app.viewModel);
};

app.interactive.updateBindings = function (element) {
ko.applyBindingsToNode(element);
};

app.interactive.init();

})(window.app = window.app || {}, jQuery);

我可以用下面的代码修复它:

ko.applyBindingsToNode(element, null, app.viewModel);

但是现在我收到这个错误:

invalid 'in' operand jQuery.template

if (templateId in jQuery['template']) 

最佳答案

我觉得自己像个白痴。我什至没有引用 jquery 模板脚本。

关于ajax - Knockout.js 不从动态 ajax 调用更新 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6819861/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com