Knockout 中当前值的名称
我正在开始淘汰赛。我不知道如何在 valueTemplate 中设置绑定。迭代器当前值的名称是什么?
<div id="knock">
<p>Name: <input data-bind="value: Name" /></p>
<table>
<thead><tr><td>Value</td></tr></thead>
<tbody data-bind="template: {name: 'valuesTemplate', foreach: Values}" />
</table>
<script type="text/html" id="valuesTemplate">
<tr><td><input data-bind="value: value" /></td></tr>
</script>
</div>
<script type="text/javascript">
var data = {"Name":"Fish","Values":["Cod","Salmon","Perch","Glish"],"Id":"lookups/1"};
var viewModel = ko.mapping.fromJS(data);
ko.applyBindings(viewModel);
</script>
I'm getting started with Knockout. I can't figure out how to setup up the binding in the valuesTemplate. What is the name of the current value of the iterator?
<div id="knock">
<p>Name: <input data-bind="value: Name" /></p>
<table>
<thead><tr><td>Value</td></tr></thead>
<tbody data-bind="template: {name: 'valuesTemplate', foreach: Values}" />
</table>
<script type="text/html" id="valuesTemplate">
<tr><td><input data-bind="value: value" /></td></tr>
</script>
</div>
<script type="text/javascript">
var data = {"Name":"Fish","Values":["Cod","Salmon","Perch","Glish"],"Id":"lookups/1"};
var viewModel = ko.mapping.fromJS(data);
ko.applyBindings(viewModel);
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 jQuery 模板内部,您可以使用
$data
或$item.data
访问当前上下文,如下所示:但是,如果您使用可观察对象,那么您将希望您的值数组包含对象而不仅仅是字符串。
Inside of a jQuery Template, you can access the current context by using
$data
or$item.data
, so like:However, if you are using observables, you are going to want your values array to contain objects rather than just strings.