如何使用 knockoutjs 在父对象上绑定 属性?

How to bind property on parent object with knockoutjs?

我正在使用 knockout.js 框架开发应用程序。我有一个这样的视图模型:

var MyViewModel= {
    Id: ko.observable(),
    CountryCode: ko.observable(),
    NormalizedAddress:
        {
            COUNTRY_CODE: ko.computed(function () { return this.CountryCode(); }),
            Street: ko.observable(),
            ZipCode: ko.observable(),
            AreaCode: ko.observable(),
            Town: ko.observable(),
            Description: ko.observable()
        }

当我 运行 我的应用程序时,我得到一个这样的异常:

0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'CountryCode'

你能帮我解决我的问题吗?

非常感谢, 马可

我使用 subscribing feature 的淘汰赛解决了我的问题。

现在我的代码是这样的:

var MyViewModel= {
    Id: ko.observable(),
    CountryCode: ko.observable(),
    NormalizedAddress:
        {
            COUNTRY_CODE: ko.observable(),
            Street: ko.observable(),
            ZipCode: ko.observable(),
            AreaCode: ko.observable(),
            Town: ko.observable(),
            Description: ko.observable()
        }
}

MyViewModel.CountryCode.subscribe(function (newValue) {
    MyViewModel.NormalizedAddress.COUNTRY_CODE(newValue);
});

所以我可以在更改 CountryCode 属性 时更改值。

谢谢