knockout.js - KnockoutJS - 显示文本框中逗号分隔的 observableArray 的内容

标签 knockout.js

这应该是一个简单的问题:我的 View 模型中有一个名为“To”的 observableArray 对象,其中包含 EmailAddress 元素。每个元素都有两个属性:DisplayName 和 Address。

我想将数组中的每个元素打印到单个输入字段,以分号分隔。我现在得到的是:

“[对象对象],[对象对象]”

如何绑定(bind)并打印属性?我尝试了各种解决方案,例如添加“value: to.DisplayName”,但无济于事。

<!-- illustration only, this is what the items in the TO array look like
     I want to print the value of DisplayName for each element -->
var EmailAddress = function(dName, addr) {
    self = this;
    self.DisplayName = dName;
    self.Address = addr;
};

<!-- viewmodel -->
var EmailModel = function (email) {
            var self = this;
            self.id = ko.observable();
            self.subject = ko.observable();
            self.body = ko.observable();
            self.from = ko.observable();
            self.to = ko.observableArray(); <-- display the DisplayName property of these elements)
            self.cc = ko.isObservable();
            self.bcc = ko.observable();
            };


<!-- print the contents of the TO array --> 
<input data-bind="value: to" type="text" />

最佳答案

编辑:第一次错过了错误,下次应该阅读整个问题。

在这种情况下有两种方法。使用计算的 obvservable,或内联 arrayMap。

这里是 a fiddle.

关于knockout.js - KnockoutJS - 显示文本框中逗号分隔的 observableArray 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11321839/

相关文章:

knockout.js - 如何防止表单提交

javascript - knockout JS : Using nested conditional if statements for virtual elements

knockout.js - KnockOut 映射分层 JS 对象

javascript - 将值传递给循环内动态创建的函数调用

javascript - ko.mapping 未定义

javascript - 从 Knockout.JS 中的数组中删除特定元素

javascript - 编辑不适用于新添加的数据 - knockout

visual-studio-2012 - 如何在 Web Developer Tools 2012.2 RTW 中禁用 knockout 智能感知

javascript - 通过 AJAX 调用问题 knockout 自定义验证

选择元素的初始值