[英]Knockout.js Dynamic HTML Binding returning [object Object]
I'm just starting out with Knockout.js, and I've got a query about dynamic HTML. 我刚刚开始使用Knockout.js,我有一个关于动态HTML的查询。
I'm creating a dynamic Questionnaire. 我正在创建一个动态的调查问卷。 This has;
这有;
I've mapped my options using the $.map function, like; 我使用$ .map函数映射了我的选项,比如;
this.options = $.map(questionOptions, function(text) {
return new Option(text)
});
I'm generating some html dynamically in the ViewModel, however when I try and concatenate say the Option Text into something like a set of Radio Buttons; 我在ViewModel中动态生成一些html,但是当我尝试连接时,将Option Text称为像一组Radio Buttons;
var htmlContent = '';
ko.utils.arrayForEach(_self.options, function (item) {
htmlContent += '<input type="radio" name="' + ko.utils.unwrapObservable(_self.questionNumber) + '" data-bind="attr: {value: value}, checked: $root.selected" />\r\n';
htmlContent += '<span>\r\n';
htmlContent += item.optionText;
//htmlContent += ko.utils.unwrapObservable(item.optionText); // Doesn't Work
//htmlContent += item.optionText(); // Doesn't Work
htmlContent += '</span>\r\n';
});
return htmlContent;
I end up with a bunch of; 我结束了一堆;
[object Object]
I've tried a couple of alternatives and gotten a bit stuck with where to go.. 我已经尝试了几种替代品,并且有点陷入困境......
I'm not sure how to use Templates, as I'm planning on having Text Boxes, Radio Buttons, Drops Downs, Lists, etc all together. 我不知道如何使用模板,因为我计划将文本框,单选按钮,下降,下载等组合在一起。 But maybe my knowledge is just lacking here!?
但也许我的知识在这里缺乏!?
Here's a jsFiddle with some example code; 这是一个带有一些示例代码的jsFiddle;
http://jsfiddle.net/PGallagher69/wA6mQ/21/ http://jsfiddle.net/PGallagher69/wA6mQ/21/
Any ideas? 有任何想法吗?
Your optionText
is indeed an Option
object. 您的
optionText
确实是一个Option
对象。
Try: 尝试:
htmlContent += item.optionText.optionText;
UPDATE: 更新:
The real problem lies here: 真正的问题在于:
function PagesViewModel(pageNo, pageHeader, questions) {
this.pageNumber = ko.observable(pageNo);
this.pageHeaderText = ko.observable(pageHeader);
this.questionItems = ko.observableArray($.map(questions, function (n) {
return [new QuestionViewModel(n.questionType, n.questionNumber, n.questionText, n.pointsBudget, n.options)]
}));
}
When you're creating your PagesViewModel
, the questions
parameter is already an array of QuestionViewModel
objects, and by assigning this.questionItems
with your custom function, you're re-creating the QuestionViewModel
, passing it n.options
that is already an array of Option
objects thus re-wrapping it again with an Option
object, and that is why you're eventually gonna need item.optionText.optionText
instead of simply item.optionText
. 在创建
PagesViewModel
, questions
参数已经是一个QuestionViewModel
对象数组,通过使用自定义函数指定this.questionItems
,您将重新创建QuestionViewModel
,并将n.options
传递给已经是一个数组Option
这样的对象再次与重新包装它Option
对象,这就是为什么你最终会需要item.optionText.optionText
而不是简单的item.optionText
。
Try this: 尝试这个:
function PagesViewModel(pageNo, pageHeader, questions) {
this.pageNumber = ko.observable(pageNo);
this.pageHeaderText = ko.observable(pageHeader);
this.questionItems = ko.observable(questions);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.