Knockout JS 中的主细节
我做错了什么?我正在尝试创建一个简单的主详细信息视图,如“规范 MVVM”示例。
这是 JSfiddle 中不起作用的简化示例: http://jsfiddle.net/UJYXg/2/
我希望在文本框中看到所选“项目”的名称,但它却显示“可观察”?
这是我的违规代码:
var list = [ { name: "item 1"} , { name: "Item 2" }];
var viewModel = {
items : ko.observableArray(list),
selectedItem : ko.observable(),
}
viewModel.setItem = function(item) {
viewModel.selectedItem(item);
}
ko.applyBindings(viewModel);
以及 HTML
<ul data-bind="foreach: items">
<li>
<button data-bind="click: $root.setItem, text:name"></button>
</li>
</ul>
<p>
<input data-bind="value:selectedItem.name" />
</p>
What am I doing wrong? I am trying to create a simple master details view a la the 'canonical MVVM' example.
Here's a simplified example in JSfiddle that doesn't work: http://jsfiddle.net/UJYXg/2/
I would expect to see the name of the selected 'item' in the textbox but instead it says 'observable'?
Here's my offending code:
var list = [ { name: "item 1"} , { name: "Item 2" }];
var viewModel = {
items : ko.observableArray(list),
selectedItem : ko.observable(),
}
viewModel.setItem = function(item) {
viewModel.selectedItem(item);
}
ko.applyBindings(viewModel);
And the HTML
<ul data-bind="foreach: items">
<li>
<button data-bind="click: $root.setItem, text:name"></button>
</li>
</ul>
<p>
<input data-bind="value:selectedItem.name" />
</p>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你们真的很亲近。只需要做
value: selectedItem().name
或更好地使用with
绑定来更改您的范围。此外,您引用的脚本稍微过时了(在 2.0 中,单击将数据作为第一个参数传递)。示例如下: http://jsfiddle.net/rniemeyer/acUDH/
You are really close. Just need to do
value: selectedItem().name
or better use thewith
binding to change your scope. Also, the script that you are referencing is slightly out-of-date (in 2.0 click passes the data as the first arg).Sample here: http://jsfiddle.net/rniemeyer/acUDH/