- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
是否:
1) 遍历 DOM(从 ng-app
开始),如果它看到带有指令的 DOM 节点,则使用相应的模型值更新该 DOM 节点。
或
2) 在遍历 $$watchers
列表时,如果它检测到变化,它会引用它需要更新的 DOM 节点,所以它只是这样做(而不是运行通过整个 DOM,使用这种方法,它会存储和使用对 $$watchers
中节点的引用。
最佳答案
更多的是第二种方法。
Angular 通过指令完成所有繁重的工作。您在 Angular 中使用的几乎所有内容都是指令:
<div ng-app>
<div ng-controller>
<button ng-click>
<!-- input is actually a directive -->
<input ng-model="foo" />
所有这些小指令都获得了对它们所附加的 DOM 元素的引用,以及一个 $scope
对象。 这些指令只是注册一个回调,以便在发生变化时执行。
正如您已经注意到的,这些称为观察者。
作用域是分层的,因此总有一个相关对象树构成您的应用程序状态。当 $digest
循环启动时,它会递归遍历树以查找更改,并触发回调(也称为观察者)。
然后回调可以选择做任何它想做的事。只是在大多数情况下,它是在更新 DOM。
归根结底,它的发生过程并没有什么神奇之处。只是一个回调结构,当某些事情发生变化时会执行。
这是一个自定义指令的愚蠢示例,它注册一个观察者并在某些属性发生变化时更新 DOM。
(function(){
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
function updateValue(){
return {
restrict:'A',
link: function(scope, elem, attrs){
elem.on('click', function(){
//You would never actually do this, but
// it's a demo
scope[attrs.updateValue] = "rgb(" +
getRandomInt(0, 255) + "," +
getRandomInt(0, 255) + "," +
getRandomInt(0, 255) + ")";
//kick off a digest loop manually
// this is similar to what a lot of angular
// directives do.
scope.$digest();
});
}
};
}
function sillyDomChangeOn(){
return {
restrict:'A',
link: function(scope, elem, attrs){
scope.$watch(attrs.sillyDomChangeOn, function(newVal, oldVal){
elem.css('color', newVal);
});
}
};
}
angular.module('my-app', [])
.directive('updateValue', updateValue)
.directive('sillyDomChangeOn', sillyDomChangeOn);
}());
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css">
<div ng-app="my-app" class="container">
<button type="button" class="btn btn-primary" update-value="randomVal">Update Value</button>
<h3>Value: <code>{{randomVal}}</code></h3>
<div class="well" silly-dom-change-on="randomVal">
<h3>This is just a random DIV</h3>
<div>
</div>
关于javascript - $digest 循环后 Angular 如何更新 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33908800/
如何将最初使用 Digest::MD5.digest('abc') 编码的哈希字符串转换为以下格式的十六进制字符串 Digest::MD5.hexdigest(' abc')?不同之处在于,十六进制字
我正在尝试制作一个应用程序,用户可以在其中将音乐上传、下载和流式传输到 Amazon Web Services、Simple Storage Services (AWS-S3)。 我试图解决的一个问题
每当我运行测试或启动 Rails 服务器时,我都会收到此警告。 当我从 .rvm 文件夹运行 grep 时,我看到以下内容: grep -R 'Digest::Digest' . ./rubies/r
我收到以下错误 Digest::Digest is deprecated; use Digest 当我尝试启动我的 Rails 服务器时。我试图在我的源代码中搜索 Digest::Digest 但我没
我在解决这个问题时遇到了一些麻烦。我正在尝试编写一个通用函数,它可以接受任何 digest::Digest并吐出计算摘要的字符串形式(“十六进制字符串”)。 这是the non-generic ver
当我尝试使用我的应用程序启动 Tomcat6 时(我正在尝试从 Tomcat7 移植到 Tomcat6),我遇到了一个奇怪的解析问题。 Digester 本身不会声明错误,直到遇到意外的文件结尾 -
使用 Digest::SHA 有什么优势吗?超过Digest::SHA1或相反亦然?两者似乎都得到了维护,但我不认为 Digest::SHA1 与 Digest::SHA 一起存在的原因。 最佳答案
例如。当我用每个函数计算哈希时,我得到不同的结果。 PKI::PKI.digest("hola", "MD5") digest::digest("hola", "md5") 最佳答案 对摘要使用ser
在 AngularJS 范围内,$apply() 在每个事件处理程序(输入指令的 keydown/input 事件,选择指令的更改事件等)和其他一些情况下被调用。 见 small example .尽
我将从这个 XML/RDF 中提取值: Church of S. Giuda Taddeo or S. Onofrio - Gaeta Chiesa S. Giuda Taddeo
我得到java.lang.NoClassDefFoundError: org/apache/commons/digester/Digester 错误,我被这个错误困扰了一个多月。我已经尝试了所有可用的
我正在为客户提供一个片段,以粘贴到他们引用我的 application.js 文件的静态 html 中。 因为它位于我无法控制的页面上,而且我不想在每次发布时都要求客户更新他们的代码片段,所以我想知道
我正在尝试在 Linux 中使用 curl 更改基于 Onvif 的相机的日期。我已经在 c# 中成功完成并使用摘要身份验证,但我被困在这种情况下。 我正在使用以下命令: curl -u "admin
我正在尝试通过 Eclipse ide 生成 jasper 报告,但是当我执行以下代码行时: jasperReport = JasperCompileManager.compileReport("re
我目前正在学习 Michael Hardtl 的 Rails 教程,在第 7 章中有一段以 require 'digest' 开头的代码。 .我跑sudo gem install digest并得到无
我有一个 perl 脚本,它使用 Digest::MD5 md5($data) 来获取 16 字节摘要(以不可读的形式 - 二进制),并且该摘要用于加密数据。现在我必须在 java 中反转上述过程,即
我使用 $http.get() 获取数据并将其作为参数传递给自定义过滤器以获取过滤数据。但它正在给予错误:$digest 已在进行中。 有谁知道如何避免此错误或以不同的方式实现相同的目标? var m
除了以纯文本形式发送凭据之外,摘要式身份验证与基本身份验证有何不同? 最佳答案 主要区别在于它不需要以明文方式通过网络发送用户名和密码。它也不受重放攻击的影响,因为它使用来自服务器的一次性号码。 服务
在签名的 apk 中,会有一个 META-INF/MANIFEST.MF 文件。在这个文件中,我们可以找到像这样的摘要: Name: res/layout/main.xml SHA1-Digest:
我是这个论坛的新手。乍一看,这个社区看起来棒极了。我有一个简单的问题。我正在使用 log4j 进行记录并且是 tomcat 的新手。我找到了设置日志规则的文件,文件名:server.log4j-con
我是一名优秀的程序员,十分优秀!