我有类似的功能
function Configuration(data) {
var self = this;
self.configKey = data.pKey;
self.configName = data.configName;
self.configNumber = data.modelnumber;
self.configMTP = ko.observable(data.mTP);
self.configMDP = ko.observable(data.mDP);
}
我的视图模型像
function AppViewModel() {
var self = this;
self.Configurations = ko.observableArray([]);
self.selConfig = ko.observable();
}
绑定就像低于
<select data-bind="options:categories,optionsCaption:'All',value:selCatgy ">
</select>
视图模型中的Configurations数组具有配置对象的列表。当用户选择特定配置时,选定的"selConfig"属性将使用选定的配置对象进行更新。。现在,如果我想从"selConfig"绑定屏幕中的其他属性(如configMTP、configMDP),可以这样做吗?
我做了下面这样的事。我们还有其他方法可以做到这一点吗?
<!-- ko foreach:selConfig-->
<pre data-bind="text: configMTP"></pre>
<input data-bind="value: configMDP" />
<input data-bind="value: configName" />
<!-- /ko -->
谢谢,
Praveen。
如果要绑定到一个对象属性,则需要使用with
绑定而不是foreach
:
<!-- ko with: selConfig -->
<pre data-bind="text: configMTP"></pre>
<input data-bind="value: configMDP" />
<input data-bind="value: configName" />
<!-- /ko -->