$apply() $digest()
理解Angular中的$apply()以及$digest()
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body ng-app="myApp">
<div ng-controller="MessageController">
Delayed Message: {{message}}
</div>
你应该使用$timeout service来代替setTimeout(),因为前者会帮你调用$apply(),让你不需要手动地调用它
<script src="angular.min.js"></script>
<script>
angular.module('myApp',[]).controller('MessageController', function($scope) { $scope.getMessage = function() {
/*
setTimeout(function() {
$scope.message = 'Fetched after 3 seconds';
console.log('message:'+$scope.message);
}, 2000);
*/ setTimeout(function() {
$scope.$apply(function() {
//wrapped this within $apply
$scope.message = 'Fetched after 3 seconds';
console.log('message:' + $scope.message);
});
}, 2000);
} $scope.getMessage(); });
</script>
</body>
</html>
随机推荐
- WPF 程序中启动和关闭外部.exe程序
当需要在WPF程序启动时,启动另一外部程序(.exe程序)时,可以按照下面的例子来: C#后台代码如下: using System; using System.Collections.Generic; ...
- 某deed笔试题
1. 删除ra,输入s,然后从前往后扫,遇到直接删除,O(n),算水题吧. 2. 矩阵乘法,看完题,感觉这么简单,估计有什么套路,仔细再读一遍,发现真是水题,50*50*50=125000,在2s时 ...
- Wix: Using Patch Creation Properties - Small Update
Source Reference: wix help document -- WiX Toolset License Using Patch Creation Properties A patch ...
- 生产者消费者问题c语言实现
#include <stdio.h> #include <process.h> #include <Windows.h> //信号量与关键段 CRITICAL_SE ...
- Nginx 域名转发
例如访问www.b.cn直接跳到www.a.cn上去,又不想多域名捆绑一个目录. server { listen 80; server_name www.b.cn; rewrite ^/(.*)$ h ...
- ASP.NET MVC5 PagedList分页示例
ASP.NET MVC是目前ASP.NET开发当中轻量级的Web开发解决方案,在ASP.NET MVC概述这篇译文当中,已经详细的介绍了ASP.NET MVC与Web Forms的区别以及各自的适用场 ...
- iframe 适用高度
contentWindow 兼容各个浏览器,可取得子窗口的 window 对象. contentDocument Firefox 支持,> ie8 的ie支持.可取得子窗口的 document ...
- 006.Compile方法
Delphi procedure Compile; 类型:procedure 可见性:public 所在单元:System.RegularExpressionsCore 父类:TPerlRegEx 此 ...
- 解决lucene 重复索引的问题
在使用Lucene过程中,会发现当我们为添加新的Document时,会产生重复现象(两次添加同一个Document),毕竟Lucene中没有像数据库中一样,有键可以区分.不过我们可以通过为Docume ...
- C语言基础知识-循环结构
用while打印出1~100之间7的倍数 int i = 1; while循环是当条件表达式的结果为真时,执行大括号里面的循环体,重复执行直到条件表达式的结果为假时结束循环. w ...