以下代码尝试将模型的可观察属性绑定到模板。在运行应用程序时,单选按钮上的数据显示完美,但单击时不会更改模型属性值。
<script type="text/html" id="RadioButton">
<input type = "radio" data-bind = "attr: {'name': ko.computed(function() { return $parents[0].QuestionID().toString(); }), 'value': ko.computed(function() { return $parents[0].OptionStatement().toString(); }) }, checked: $parents[1].Answer_RadioList().TextResponse() "/>
<span data-bind = "text: $parents[1].Answer_RadioList().TextResponse()"></span>
</script>
应用程序的模型如下所示:
function QuestionList(T) { this.QuestionID = ko.observable(T.QuestionID); this.Question = ko.observable(T.Question); this.SurveyID = ko.observable(T.SurveyID); this.CategoryID = ko.observable(T.CategoryID); this.SubCategoryID = ko.observable(T.SubCategoryID); this.ParentID = ko.observable(T.ParentID); this.IsMandatory = ko.observable(T.IsMandatory); this.IsARadioButtonControl = ko.observable(T.IsARadioButtonControl); if (T.Answer_RadioList) { this.Answer_RadioList = ko.observable(new Answer_RadioList(T.Answer_RadioList)); } this.HasInternalControls = ko.observable(T.HasInternalControls); this.InternalControlTypeID = ko.observable(T.InternalControlTypeID); this.OptionListCollection = ko.observableArray([]); this.InternalQuestionListCollection = ko.observableArray([]); } function OptionList(T) { this.OptionID = ko.observable(T.OptionID); this.OptionStatement = ko.observable(T.OptionStatement); this.QuestionID = ko.observable(T.QuestionID); this.IsMandatory = ko.observable(T.IsMandatory); this.IsTabularControl = ko.observable(T.IsTabularControl); this.ControlTypeID = ko.observable(T.ControlTypeID); this.ControlList = ko.observable(new ControlList(T.ControlList)); this.TabularControl = ko.observableArray([]); this.AxisSummation = ko.observable(new AxisSummation(T.AxisSummation)); this.IsADropDownList = ko.observable(T.IsADropDownList); this.Answer_OptionList = ko.observable(new Answer_OptionList(T.Answer_OptionList)); } function Answer_RadioList(T) { this.Answer_RadioListID = ko.observable(T.Answer_RadioListID); this.QuestionID = ko.observable(T.QuestionID); this.TextResponse = ko.observable(T.TextResponse); }
答案 0 :(得分:1)
您需要删除checked: $parents[1].Answer_RadioList().TextResponse()
中的括号:
checked: $parents[1].Answer_RadioList().TextResponse
否则,您会给出一个值而不是一个可观察的值。