我有像下面一樣的嵌套視圖模型。我試圖從包含的視圖模型(子)訪問容器視圖模型中的值。當modelA.prop1試圖獲取mainVM.prop1值時,我得到了未定義的錯誤。謝謝你的幫助。knockout.js訪問包含的視圖中的容器模型屬性Model
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);
});