我已经看到控制台日志observables的意见,但它似乎并没有为我工作。另外我的应用程序没有启动我预期的默认变量。 这两个问题是在一起,因为我怀疑他们有某种关系。淘汰赛控制台日志和观察未定义
HTML
<select data-bind="options: widgets, optionsText: 'name', value: current_widget, optionsCaption: 'Choose...'"></select>
<input data-bind="value: current_widget() ? current_widget().name : 'nothing'" />
的Javascript
var cdta = {};
$(document).ready(function(){
cdta.widgets_data = [{ "name": "foo", "id": "1"},{ "name": "bar", "id": "2"},{ "name": "bash", "id": "3"},{ "name": "baq","id": "4"}];
cdta.local = {};
cdta.local.current_widget = { "name": "foo", "id": "1"};
alert('current_widget starting value should be: ' + cdta.local.current_widget.name);
function app() {
var self = this;
//widgets
self.widgets = ko.observableArray(cdta.widgets_data);
// which widget to display from a local source
alert('about to asign a current_widget named:' + cdta.local.current_widget.name);
self.current_widget = ko.observable(cdta.local.current_widget);
}
ko.applyBindings(new app());
alert('after applying bindings name of current widget: ' + current_widget().name);
//expecting foo
//but doesn’t alert because current_widget isnt defined
});
@AlfeG感谢的是解决了可变问题 – 2012-02-09 23:07:39
@Roman Bataev感谢也为第2部分有关optionsCaption - 这是一个真正的疑难杂症 – 2012-02-09 23:08:39