Knockout学习笔记之二($root,$parent及$data的区别)

以下是我从Google上找到的一个例子,非常生动形象,我修改了部分代码,具体内容如下:

对于$root 与$parent的区别:

  • $root refers to the view model applied to the DOM with ko.applyBindings;

        译:$root 是指ViewModel所应用于ko.applyBindings时所使用的DOM;

  • $parent refers to the immediate outer scope;

    译:$parent 是指当前DOM元素直接的外部父类(只有一层);

Or, visually, from $data's perspective:

 Knockout学习笔记之二($root,$parent及$data的区别)_第1张图片

 

Or, in words of the relevant documentation:

  • $parent: This is the view model object in the parent context, the one immeditely outside the current context.
  • $root: This is the main view model object in the root context, i.e., the topmost parent context. It’s usually the object that was passed to ko.applyBindings. It is equivalent to $parents[$parents.length - 1].

对于三者的区别($root,$parent及$data):




    
    
    
    
    

    
$root $parent $data
View Code

具体页面呈现:

Knockout学习笔记之二($root,$parent及$data的区别)_第2张图片

The $root is always the same. The $parent is different, depending on how deeply nested you are.

译:$root经常是相同的,而$parent是不同的,而这种不同主要取决于你嵌套的深度。

转载于:https://www.cnblogs.com/sccd/p/5981998.html

你可能感兴趣的:(Knockout学习笔记之二($root,$parent及$data的区别))