嵌套模板
我有以下代码:
function Session(name, index) {
this.Messages = [];
this.Name = name;
this.Index = index;
}
var vm = {};
vm.Sessions = ko.observableArray([new Session("Foo", 0), new Session("Bar", 1)]);
ko.applyBindings();
vm.Sessions()[0].Messages.push("Hello foo");
沿着这个视图:
<div data-bind="template: {name: 'TopTemplate', foreach: Sessions}">
</div>
<script type="text/html" id="TopTemplate">
<p>
${$data.Name}
<ul data-bind="template: {name: 'NestedTemplate', foreach: Sessions[${$data.Index}]}"></ul>
</p>
</script>
<script type="text/html" id="NestedTemplate">
<li>
${$data}
</li>
</script>
正如您所看到的,有一个包含数组的对象。因此,我创建了可观察的会话数组,它变得可观察,包括内部属性。我在这里想要的是显示嵌套的“中继器”。
昨天我以某种方式成功执行了这个脚本。有趣的是,没有显示属性名称,即 Sessions[${$data.Index}].Messages
。不幸的是,我删除了那个测试脚本。
现在我尝试重新创建,但没有成功。
附言。问题是我不想在没有显示相关属性的情况下让它工作。我只想让嵌套模板发挥作用。
I have the following code:
function Session(name, index) {
this.Messages = [];
this.Name = name;
this.Index = index;
}
var vm = {};
vm.Sessions = ko.observableArray([new Session("Foo", 0), new Session("Bar", 1)]);
ko.applyBindings();
vm.Sessions()[0].Messages.push("Hello foo");
Along this view:
<div data-bind="template: {name: 'TopTemplate', foreach: Sessions}">
</div>
<script type="text/html" id="TopTemplate">
<p>
${$data.Name}
<ul data-bind="template: {name: 'NestedTemplate', foreach: Sessions[${$data.Index}]}"></ul>
</p>
</script>
<script type="text/html" id="NestedTemplate">
<li>
${$data}
</li>
</script>
As you can see there is an object with the containing array. So I make the observable array of sessions and it becomes observable including the internal properties. What I want here is to display nested 'repeaters'.
Yesterday I somehow suceeded to execute this script. And what is interesting without showing property name, i.e. Sessions[${$data.Index}].Messages
. Unfortunately, I deleted that test script.
Now I've tried to recreate and it doesn't work.
PS. The thing is I don't want to make it work witout showing the relevant property. I just want to make work the nested template.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这似乎更接近您想要完成的任务:
代码:
另请参阅 this fiddle
This seems closer to what you want to accomplish:
And code:
See also this fiddle