我试图让knockout.js在ajax调用之后更新我的视图,但没有运气。我一直收到下面的错误。它在app.interactive.updateBindings
处出错。 parseBindingAttribute viewModel参数未定义。
错误:
无法解析绑定属性。消息:
ReferenceError:interactive
未定义;
属性值: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);
但现在我收到了这个错误:
'操作数jQuery.template 中的无效'
if (templateId in jQuery['template'])
答案 0 :(得分:9)
答案 1 :(得分:0)
看起来有点令人困惑。您有 app.viewModel.interactive ,然后 app.interactive 。我很可能会遗漏一些东西,但你会尝试将绑定应用到更简单的命名空间视图模型并从那里开始工作吗?