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,如需转载请自行联系原作者