English 中文(简体)
knockoutjs 模板在查看前显示模式负载/ binds
原标题:knockoutjs template displays before viewModel loads/binds

我正使用 Knockoutjs 模板。 页面上装有 html 显示的模板一瞬间, 然后在视图装入并绑定模式后, 它会正确显示 。 处理此问题的最佳方式是什么? 目前我正在将模板包在设置要显示的 div 中: 没有, 然后在应用该绑定后删除它 。

最佳回答

在脚本标记中折行您的标记 :

<script type="text/html" id="viewModelTemplate">
... your markup ...
</script>

然后有一个单一的顶级元素,包括:

<div data-bind="template: {name:  viewModelTemplate }"/>

能帮上忙

问题回答

您何时绑定查看模型? 您是否根据 AJAX 请求或事件这样做? 如果数据存在, 在显示 HTML 之前, 您页面底部的脚本标记应该运行, 并在显示 HTML 之前执行 绑定 。 如果您在显示 HTML 后需要绑定 运行, 您需要使用您的方法 。 如果您正在使用 AJAX, 我将视图模型结构成您绑定在空视图模型上, 并且拥有您在装入完成时设定的 < code> is Loaded 等属性, 那么您就可以绑定它 :

<div data-bind="visible: isLoaded">...</div>




相关问题
How to go from DOM node to viewModel object?

When the drop function is called back, this is set to the droppable DOM node (the target) and ui.draggable is the DOM node which was dragged. Is there an idiomatic way of getting the model object ...

making fields observable after ajax retrieval in knockout.js

I am wondering how I can make certain fields observables in knockout.js that I get from an ajax call without having to define the whole object in my viewmodel. Is this possible? Here is what I have ...

ASP.NET MVC Validation with jQuery $.ajax

I have a situation where I am sending data to a Controller via jQuery $.ajax, the data is a serialized json string. (MVC 3.0) It binds it fine - my controller receives the results and they are ...

Knockout.js and MVC

Just started playing with knockout.Js which is a fantastic framework Steve s really done well with that one. One thing I can t seem to do at the minute is impliment it with my Html Helpers. So for ...

Anyone using Knockoutjs with asp.net-mvc in anger? [closed]

I find it very interesting and have a prototype working based on Steve s mvc sample and another small sample from this thread. Using json.net to deserialize within the post action as I couldn t ...

热门标签