【问题标题】:Knockout: Subscribing Nested Objects淘汰赛:订阅嵌套对象
【发布时间】:2015-01-04 13:32:13
【问题描述】:

我正在尝试订阅嵌套对象,但它不起作用。这里是my fiddle。 下面也是我的代码

var ViewModel = function () {
this.data = ko.observable();
var sample = {
    id: 1,
    details: {
        name: "Johnny",
        surname: "Boy",
        othername: ""
    }
};

this.data(sample);

//This does not work
// this.data().details().name.subscribe(function (val) {
    //data().details().othername(val)
// });
 };

ko.applyBindings(new ViewModel());

这是我的html

<div data-bind="with: data">
<p>Name:
    <span data-bind="text: details.name"></span>
</p>
<p>Surname:
    <input data-bind='value: details.surname' />
</p>
<p>Other:
    <input data-bind='value: details.othername' />
</p>

【问题讨论】:

标签: jquery knockout.js subscription


【解决方案1】:

您需要使嵌套属性可观察。

例子:

name: ko.observable("Johnny")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-20
    • 2016-02-29
    • 2014-08-08
    • 2012-10-09
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多