简体   繁体   English

Knockout.js动态HTML绑定返回[object Object]

[英]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; 这有;

  • The Main Questionnaire View Model, which contains ... 主要问卷视图模型,包含......
  • An observableArray of Pages, which contains ... 一个observableArray of Pages,其中包含......
  • An observableArray of Questions, which contains ... 一个可观察的问题,包含......
  • An Array of Options. 一系列选项。

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 . 在创建PagesViewModelquestions参数已经是一个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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM