简体   繁体   English

如何从父视图模型访问孙子视图模型?

[英]How to access grandchild viewmodel from parent viewmodel?

In my webapp i am creating three viewmodel which are nested like first viewmodel is parent of second , second is parent of third. 在我的web应用程序中,我正在创建三个嵌套的viewmodel,就像第一个viewmodel是second的父级,second是第三名的父级一样。 now if i want to display value of third view model it is giving me error that it is not define . 现在,如果我想显示第三视图模型的值,则会给我未定义的错误。 In my code LoanRateViewModel is parent whose child is Term and Tier is child of Term . 在我的代码LoanRateViewModel是父母,其孩子是TermTier是儿童Term

function Tier(data){
    var self = this;
    self.tierValue = ko.observable(data.tierValue || "");
    self.rate = ko.observable(data.rate || "121212");
    self.simplerate = ko.observable(data.simplerate || "121212");
    self.compoundrate = ko.observable(data.compoundrate || "12212");
    } 

function Term(data) {
    var self = this;
    self.Tiers = ko.observableArray([]);
    self.loanterm = ko.observable(data.loanterm || "12");
    self.termIdentifier = ko.observable(data.termIdentifier || "12");
};

here is jsfiddle link DEMO 这是jsfiddle链接演示

How can i call the grand child is it possible through knockout js ? 我怎么称呼大孩子,通过淘汰赛js有可能吗?

The problem is unfortunately not in the code you posted*, but in the fiddle. 不幸的是,问题不在您发布的代码中*,而是在小提琴中。

What you want is certainly possible, though as mentioned in comments you'd need proper nesting to reach grand-children, because they will always be in the context of a certain parent ( Term ). 当然,您想要的东西是可能的,尽管如注释中所述,您需要适当的嵌套才能到达孙子女,因为他们将始终处于某个父母( Term )的上下文中。 I've (mostly) fixed your jsfiddle , fixing issues until there were no errors left. 我(大部分)已经修复了jsfiddle ,修复了问题,直到没有错误为止。 Some things that would answer your "question" are: 可以回答您“问题”的一些事情是:

  • Make sure you properly close DOM elements (eg table and tbody ); 确保正确关闭DOM元素(例如tabletbody );
  • Make sure you refer to $root for methods that are on the root (eg addTier ); 确保您引用$root$root的方法(例如addTier );
  • In tables, make sure each row has equal number of cells (or use colspan ), or your results will look weird. 在表格中,确保每一行具有相等数量的单元格(或使用colspan ),否则结果将看起来很奇怪。

* It'd be nice if you edit your question, and included the relevant (preferably well-formatted) code, so the question can remain useful for others with a similar problem. *如果您编辑问题并包含相关的(最好是格式正确的)代码,那将是很好的选择,因此该问题对于其他遇到类似问题的人仍然有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM