敲除绑定 select 与具有可观察属性的自定义对象
Knockout bind select with custom object with observable properties
我正在尝试将 <select>
与 Knockout 绑定。在我的 ViewModel 中,我有 2 个不同的对象,每个对象都具有可观察的属性。
第一个对象是 Property
,它有一个 headquarter_id
作为一个 ko.observable()
。
第二个对象是 Headquarter
,它有一个 id
和一个 name
,两者都是 ko.observable()
.
我想做的是将 select 与 ko.observableArray()
个 Headquarter
对象绑定,如下所示:
<select id="headquarters" data-bind="options: HeadquarterOptions, optionsText: name, optionsValue: id, optionsCaption: '--Select--', value: Property().headquarter_id"></select>
但我不断收到:
Uncaught ReferenceError: Unable to process binding "options: function (){return HeadquarterOptions }"
Message: id is not defined
这是我的 ViewModel 的样子:
var Property = function () {
var self = this;
self.headquarter_id = ko.observable();
}
var Headquarter = function (id, name) {
var self = this;
self.id = ko.observable(id);
self.name = ko.observable(name);
}
var headquarterOptions = [
new Headquarter(1, "hq 1"),
new Headquarter(2, "hq 2"),
new Headquarter(3, "hq 3"),
]
var PropertiesViewModel = function (app, dataModel) {
var self = this;
self.Property = ko.observable(new Property());
self.HeadquarterOptions = ko.observableArray(headquarterOptions);
}
ko.applyBindings(PropertiesViewModel);
我怎样才能做到这一点?
这是我当前的 fiddle:http://jsfiddle.net/juandozco/dzwnb05b/
optionsValue
和 optionsText
应声明为函数而不是值 (http://knockoutjs.com/documentation/options-binding.html):
optionsText: function(item) {return item.name; }
optionsValue: function(item){ return item.id; }
查看更新后的 fiddle:http://jsfiddle.net/dzwnb05b/3/
好了http://jsfiddle.net/dzwnb05b/4/
<select class="form-control" id="headquarter" data-bind="options: HeadquarterOptions, optionsText: 'name', optionsValue: 'id', optionsCaption: '--Select--', value: Property().headquarter_id"></select>
您在名称和 ID 两边缺少单引号。
我正在尝试将 <select>
与 Knockout 绑定。在我的 ViewModel 中,我有 2 个不同的对象,每个对象都具有可观察的属性。
第一个对象是 Property
,它有一个 headquarter_id
作为一个 ko.observable()
。
第二个对象是 Headquarter
,它有一个 id
和一个 name
,两者都是 ko.observable()
.
我想做的是将 select 与 ko.observableArray()
个 Headquarter
对象绑定,如下所示:
<select id="headquarters" data-bind="options: HeadquarterOptions, optionsText: name, optionsValue: id, optionsCaption: '--Select--', value: Property().headquarter_id"></select>
但我不断收到:
Uncaught ReferenceError: Unable to process binding "options: function (){return HeadquarterOptions }"
Message: id is not defined
这是我的 ViewModel 的样子:
var Property = function () {
var self = this;
self.headquarter_id = ko.observable();
}
var Headquarter = function (id, name) {
var self = this;
self.id = ko.observable(id);
self.name = ko.observable(name);
}
var headquarterOptions = [
new Headquarter(1, "hq 1"),
new Headquarter(2, "hq 2"),
new Headquarter(3, "hq 3"),
]
var PropertiesViewModel = function (app, dataModel) {
var self = this;
self.Property = ko.observable(new Property());
self.HeadquarterOptions = ko.observableArray(headquarterOptions);
}
ko.applyBindings(PropertiesViewModel);
我怎样才能做到这一点?
这是我当前的 fiddle:http://jsfiddle.net/juandozco/dzwnb05b/
optionsValue
和 optionsText
应声明为函数而不是值 (http://knockoutjs.com/documentation/options-binding.html):
optionsText: function(item) {return item.name; }
optionsValue: function(item){ return item.id; }
查看更新后的 fiddle:http://jsfiddle.net/dzwnb05b/3/
好了http://jsfiddle.net/dzwnb05b/4/
<select class="form-control" id="headquarter" data-bind="options: HeadquarterOptions, optionsText: 'name', optionsValue: 'id', optionsCaption: '--Select--', value: Property().headquarter_id"></select>
您在名称和 ID 两边缺少单引号。