博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AngularJs $scope 里面的$apply 方法和$watch方法
阅读量:6962 次
发布时间:2019-06-27

本文共 925 字,大约阅读时间需要 3 分钟。

Angular $scope 里面的$apply 方法

Scope提供$apply方法传播Model变化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<!DOCTYPE html>
<html>
    
<head>
        
<meta http-equiv=
"Content-Type" 
content=
"text/html; charset=utf-8" 
/>
        
<title>无标题文档</title>
        
<script type=
"text/javascript" 
src=
"angular.min.js"
></script>
    
</head>
    
<body>
      
<div ng-app=
"myApp"
>
 
          
<div ng-controller=
"firstController"
>
              
{
{name}}
 
 
          
</div>
 
 
      
</div>
      
<script type=
"text/javascript"
>
          
var 
app = angular.module(
"myApp"
, []);
          
app.controller(
'firstController'
,[
'$scope'
,
function
($scope){
 
              
setTimeout(
function
(){
 
                  
$scope.$apply(
function
(){
                      
$scope.name=
'李四'
;
                  
});
 
 
              
}, 2000);
              
$scope.name=
'张三'
;
 
          
}]);
 
 
 
      
</script>
        
    
</body>
</html>

  2s 后更新name的值为李四

 

2. ng-click使用, 如下图,定义了一个changeName方法,点击后修改名字为王五

 

timeout的使用,如下图,不需要再写setTimeout.

 

本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/p/7090786.html,如需转载请自行联系原作者

你可能感兴趣的文章
[LeetCode] Rotate Function
查看>>
iOS - 更轻量级的 AppDelegate - 面向服务设计
查看>>
什么样的爬虫才是好爬虫:Robots协议探究
查看>>
CentOS6.6设置静态ip
查看>>
tomcat 部署多个项目的技巧
查看>>
yii2使用多个数据库的案例
查看>>
[LintCode/LeetCode] Search Insert Position
查看>>
Node处理http跨域请求
查看>>
Gradle for Android 第五篇( 多模块构建 )
查看>>
框架 也可以这样用 easy
查看>>
LCUI 1.1.0 Beta 发布,C 的图形界面库
查看>>
recyclerView 遇到IndexOutOfBoundsException 及解决
查看>>
微信支付教程系列之现金红包
查看>>
mybatis中resultMap与resultType的使用说明
查看>>
Base64
查看>>
3、MySQL的数据类型介绍
查看>>
ubuntu apache2配置详解
查看>>
MariaDB(mysql)之半同步复制及复制过滤器的使用
查看>>
WCF Ria Service 理解制图版本【待续】
查看>>
【IOS】高仿糗事百科客户端(基本实现了除注册,评论之外的功能)
查看>>