我正在使用 MVC 4 来玩弄前端架构的 Knockout。我目前的前端有以下脚本......
<script type="text/javascript" src="~/Scripts/knockout-2.2.0.js"></script>
<script type="text/javascript" src="~/Scripts/knockout.mapping-latest.js"></script>
<script type="text/javascript">
// This is a simple *viewmodel* - JavaScript that defines the data and behavior of your UI
function AppViewModel() {
this.merchantName = ko.observable("Store");
}
// Activates knockout.js
ko.applyBindings(new AppViewModel());
</script>
<p>Merchant name: <strong data-bind="text: merchantName"></strong></p>
问题在于,merchantName 属性的数据未显示在 HTML 中。我在 Knockout 教程页面 http://learn.knockoutjs.com/#/?tutorial=intro 上运行相同的代码 一切正常。您认为可能是什么问题。另外,我在前端开发方面相对较新。除了 chrome 调试器之外,调试 knockout 问题的最佳方法是什么?
谢谢!
最佳答案
您可以将以下 2 个选项用于 ko.applyBindings
:
- 在 DOM 顶部添加
document.onload
检查 - 在 DOM 加载后的底部,没有任何检查
来自文档:http://knockoutjs.com/documentation/observables.html
You can either put the script block at the bottom of your HTML document, or you can put it at the top and wrap the contents in a DOM-ready handler such as jQuery’s $ function.
关于javascript - 来自 Knockout.js View 模型的文本未显示在 html 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25267375/