且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

敲除$数据绑定到HTML元素

更新时间:2023-10-08 10:21:52

一般来说,你想要创建一个 selectedPerson 可以在视图模型级别观察,然后可以执行以下操作:

Generally, you would want to create like a selectedPerson observable at the view model level and then you could do something like:

<ul data-bind="foreach: people">
    <li data-bind="click: $parent.selectedPerson">
         <span data-bind="text: name"></span>
         <div data-bind="visible: $parent.selectedPerson() === $data">
              <span data-bind="text: age"></span>
         </div>
    </li>
</ul>

如果您愿意,您一定可以使用名称周围的链接/按钮。当您单击它时, selectedPerson 将用作处理程序,并将当前数据作为其第一个参数传递。因为, selectedPerson 实际上是一个可观察的,它将以数据作为其值填充它。

You could certainly use a link/button around the name, if you like. When you click on it, selectedPerson will be used as the handler and passed the current data as its first argument. Since, selectedPerson is actually an observable, it will populate it with the data as its value.

否则,你当然可以有另一个区域显示你所做的细节:

Otherwise, you could certainly have another area to display the details where you do:

<div data-bind="with: selectedPerson">
   ....
</div>

快速提示: http://jsfiddle.net/rniemeyer/8dRZ4/