Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 11 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(1)
您可以按照以下几个方向进行操作。
一种选择是针对单独的 DOM 元素使用不同的视图模型调用 ko.applyBindings,例如:
http:// /jsfiddle.net/9abgxn8k/
在这种情况下,您需要确保元素不是彼此的祖先(不想将任何内容绑定两次)
另一种选择是使用子视图模型:
在此方法中,您可以在要与每个视图模型一起显示的区域上使用
with
绑定。您可以使用子模型或顶部模型上的标志来控制可见性。我喜欢的另一个选择是为您的“视图”提供一点结构,并执行以下操作:
http:// jsfiddle.net/rniemeyer/PctJz/
There are a few directions that you could go with this one.
One option is to call
ko.applyBindings
with distinct view models against separate DOM elements like:http://jsfiddle.net/9abgxn8k/
In this case, you would want to make sure that the elements are not ancestors of each other (don't want to bind anything twice)
Another option is to use sub view models:
In this method, you would then use
with
bindings on the areas that you want to display with each view model. You can control visibility with flags on the sub models or on the top model.Another option that I like is to give your "views" a little bit of structure and do something like:
http://jsfiddle.net/rniemeyer/PctJz/