谈angularJs中的apply()

当触发事件click()及setTimeout(),$timeout这些要更新DOM要使用apply(),不然模板更新了,但DOM更新不了。  后台可以打印出来, 详细;http://blog.csdn.net/dm_vincent/article/details/38705099

还可以简单粗暴scope。scope.$apply()

<body ng-app="myApp">  
  <div ng-controller="MessageController">  
    Delayed Message: {{message}}  
  </div>    
</body>  

angular.module('myApp',[]).controller('MessageController', function($scope) {  
      
      $scope.getMessage = function() {  
        setTimeout(function() {  
          $scope.message = 'Fetched after 3 seconds';  
          console.log('message:'+$scope.message);  
        }, 2000);  
      }  
        
      $scope.getMessage();  
      
    });  

不起作用,以下改善。

angular.module('myApp',[]).controller('MessageController', function($scope) {  
      
      $scope.getMessage = function() {  
        setTimeout(function() {  
          $scope.$apply(function() {  
            //wrapped this within $apply  
            $scope.message = 'Fetched after 3 seconds';   
            console.log('message:' + $scope.message);  
          });  
        }, 2000);  
      }  
        
      $scope.getMessage();  
      
    });  

事件类型;

<body ng-app="APP">
<script src="https://cdn.bootcss.com/angular.js/1.5.8/angular.min.js"></script>
<p>{{message}}</p><input ng-model="message" />
<div><test-dw></test-dw></div>
</body>

var appd = angular.module("APP",[]);
        appd.directive("testDw", function(){
            return{
                //restrict:"A",
                //scope:true,
                restrict:"AE",
    	          template:"<div><button>点击</button><div>{{message}}</div></div>",
                //replace:true,
                link:function(scope, element, attrs){
                    element.bind("click", function(){
                        //scope.$apply(function(){
                            scope.message="这就对啦";
                            console.log(scope.message)
                        //})
                    } )
                },
            }
        })

改善,

<body >
<p>{{message}}</p><input ng-model="message" />
<div><test-dw></test-dw></div>
<script src="https://cdn.bootcss.com/angular.js/1.4.0-beta.4/angular-1.4.0-beta.5/angular.js"></script>
<script type="text/javascript">
    var appd = angular.module("APP",[])
        .directive("testDw", function(){
            return{
                restrict:"A",
                scope:true,
                template:"<div><button>点击</button><div>{{message}}</div></div>",
                //replace:true,
                link:function(scope, element, attrs){
                    element.bind("click", function(){
                        scope.$apply(function(){
                            scope.message="这就对啦";
                        })
                    } )
                }
            }
        })
</script>
</body>

 

 

转载于:https://my.oschina.net/u/3427060/blog/1524840

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值