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方法,点击后修改名字为王五

AngularJs $scope 里面的$apply 方法和$watch方法

 

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

AngularJs $scope 里面的$apply 方法和$watch方法

 


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

相关文章:

  • 2022-01-19
  • 2022-12-23
  • 2022-01-06
  • 2022-12-23
  • 2022-12-23
  • 2021-06-19
  • 2022-12-23
  • 2021-12-19
猜你喜欢
  • 2021-05-15
  • 2021-10-12
  • 2022-12-23
  • 2022-12-23
  • 2022-01-23
  • 2022-12-23
相关资源
相似解决方案