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:
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):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head><title></title><script type="text/javascript" src="knockout-3.4.0.js"></script><style>th, td {padding: 10px;border: 1px solid gray;}</style><script type="text/javascript">window.onload = function () {vm.mainPerson(grandpa);grandpa.children.push(daddy);daddy.children.push(son1);daddy.children.push(son2);ko.applyBindings(vm);}var Person = function (name) {var self = this;self.name = ko.observable(name);self.children = ko.observableArray([]);}var ViewModel = function () {var self = this;self.name = 'root view model';self.mainPerson = ko.observable();}var vm;vm= new ViewModel(),grandpa = new Person('grandpa'),daddy = new Person('daddy'),son1 = new Person('marc'),son2 = new Person('john');</script><script type="text/html" id="person"><tr><td data-bind="text: $root.name"></td><td data-bind="text: $parent.name"></td><td data-bind="text: $data.name"></td></tr><!-- ko template: { name: 'person', foreach: children } --><!-- /ko --></script><table><tr><th>$root</th><th>$parent</th><th>$data</th></tr><!-- ko template: { name: 'person', data: mainPerson } --><!-- /ko --></table> </head> <body> </body> </html>
View Code
具体页面呈现:
The $root is always the same. The $parent is different, depending on how deeply nested you are.
译:$root经常是相同的,而$parent是不同的,而这种不同主要取决于你嵌套的深度。
Knockout学习笔记之二($root,$parent及$data的区别)相关推荐
- knockout学习笔记目录
关于knockout学习系列的文章已经写完,这里主要是做个总结,并且将目录罗列出来,方便查看.欢迎各位大神拍砖和讨论. 总结 kncokout是一个轻量级的UI类库,通过MVVM模式使前端的UI简单话 ...
- MNE学习笔记(六):Epoched data的可视化
MNE学习笔记(六):Epoched data的可视化 参考文章:https://mne.tools/stable/auto_tutorials/epochs/20_visualize_epochs. ...
- 数据库学习笔记(一) | 数据(Data)的定义
数据库学习笔记(一) | 数据(Data)的定义和种类 什么是数据(Data) 结构化数据(Structured Data) 半结构化数据(Semi-structured Data) 非结构化数据(U ...
- 神经网络与深度学习笔记汇总二
神经网络与深度学习笔记汇总二 正交化(方便调整参数) 迭代 单实数评估指标(判断几种手段/方法哪个更好) 指标选取 训练集.开发集.测试集作用与用途 评估指标 判断算法是好是坏 迁移学习 总结 往期回 ...
- 神经网络学习笔记(4)——期望与算术平均值的区别、方差与均方误差的区别
本来说直接看BP算法的代码的,但是看书的时候又确实遇到了这两个东西,所以就先记上这么一个学习笔记. 虽然这种纯数学的东西放在神经网络的学习笔记中好像也不太对,但是确实是学习神经网络的时候遇到的,所以就 ...
- knockout 学习笔记
1.激活knockout ko.applayBindings() 第一个参数是激活ko时用于声明绑定的View Model的对象 (myViewModel= {personName: 'Bob',pe ...
- knockout学习笔记
学习knockout路程中记下的一些容易遗忘和稍微难理解的知识点,学习地址"knockout.js爱整理" 1.<!–ko–>和<!–/ko–>的用法 有时 ...
- 《SAS编程与数据挖掘商业案例》学习笔记之二
继续之前的读书笔记,废话少说,直奔主题. 四.连接数据库的方式 libname形式: Oracle 链接:Libname IBMSHCU oracle user=bi password=bi sch ...
- Android学习笔记——Menu(二)
知识点: 这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu). 上下文菜单 上下文菜单提供对UI界面上的特定项或上下文框架的操作,就 ...
最新文章
- 使用 LocalReport 对象进行打印
- 武汉大学计算机学院学生寝,多彩青春一路同行——记武大计算机学院计科2班...
- 文献记录(part47)--KU-HAR: An open dataset for heterogeneous human activity recognition
- 沙漠上不小心挖了个洞,让这个地狱之门般的巨坑,燃烧了50年
- 如何判断注册用户是否已经存在(membership验证)
- MogDB存储过程事务控制与异常块
- 计算机垃圾回收的过程,计算机体系 – 垃圾收集器
- Linux学习总结(78)—— 常见开源协议讲解
- redis通用key操作
- 惠普服务器如何修改ip地址,HP网络打印机如何设置IP地址
- 启动Activity时显示空白界面的问题
- jQuery笔记——工具函数——jQuery标志
- Html设置网页背景图片
- python与r语言处理excel数据_R语言 | 读写txt、csv、excel文件
- iMeta和CGM联合报告:中科院生态中心邓晔-气候变化驱动核心微生物的迁移(5.4晚8点)...
- matlab 验证两个电阻并联误差会变小
- 部门换届推文文字_毕业季,换届忙
- 自己写的忽略大小写查找字符串的子串的函数
- PADS Logic原理图中如何替换自定义的模板
- Dota2电竞数据API接口 - 【战队基本信息】API调用代码
热门文章
- 《java程序员修炼之道》pdf书籍
- 【perl】simpleHTTP
- 常用开发环境搭建配置教程(OneStall)
- remove()方法
- web.xml 配置文件 超详细说明!!!
- 什么是慢查询?如何通过慢查询日志优化?
- jdbc执行sql 不支持“variant”数据类型。
- Ubuntu16.04下Mongodb官网安装部署步骤(图文详解)(博主推荐)
- IPv6 相关的工作简介
- [Leetcode][第111题][JAVA][BFS][二叉树的最小深度][BFS][递归]