我有嵌套的视图模型,如下所示。我正在尝试从包含的视图模型(子)访问容器视图模型中的值。当 modelA.prop1 试图获取 mainVM.prop1 值时出现未定义的错误。谢谢你的帮助。
function mainVM() {
var self = this;
//chain associated view models
self.modelA = new modelA();
self.modelB = new modelB();
self.prop1 = ko.observable("some value from mainVM.prop1");
}
function modelA(){
var self = this;
self.prop1 = ko.observable(mainVM.prop1); //I'd like to get value in containing view model above
}
function modelB(){....}
$(function () {
var viewModel = new mainVM();
ko.applyBindings(viewModel);
});