zoukankan      html  css  js  c++  java
  • angular随笔

      angular个别情况scope值不能改变或者不能绑定【如:指令内ctrl.$setViewValue()不能直接改变input的val值,该处需要使用scope.$apply】

    如之前写的简单指令

    var app = angular.module('hpapp', []);
    app.directive('inputempty', function() {
        return {
            restrict: 'A',
            require: 'ngModel',
            link: function(scope, elem, attrs, ctrl) {
                var close = '<span class="clear"></span>';
                elem.next().bind('click', function() {
                    ctrl.$setViewValue('');
                    ctrl.$render();
                });
            }
    
        };
    });

    改变为

    var app = angular.module('hpapp', []);
    app.directive('inputempty', function() {
        return {
            restrict: 'A',
            require: 'ngModel',
            link: function(scope, elem, attrs, ctrl) {
                var close = '<span class="clear"></span>';
                elem.next().bind('click', function() {
                ctrl.$apply(function(){
                        ctrl.$setViewValue('');
                    });
                });
            }
    
        };
    });            

    以及下边的情况,在angular内部使用setInterval()以及setTimeout()都不能直接绑定

    angular.module('app',[])
        .controller('testController', function($scope) {  
          
          $scope.test = function() {  
            setTimeout(function() { 
                $scope.text = 'test';   
                console.log($scope.text );  
            }, 2000);  
          }  
            
          $scope.test ();  
          
        });  
    angular.module('app',[])
        .controller('testController', function($scope) {  
          
          $scope.test = function() {  
            setTimeout(function() {  
              $scope.$apply(function() {
                $scope.text = 'test';   
                console.log($scope.text );  
              });  
            }, 2000);  
          }  
            
          $scope.test ();  
          
        });  
  • 相关阅读:
    Mac下mysql出现错误:ERROR 1055 (42000)
    单表查询
    外键的变种 三种关系
    Java8中Lambda表达式详解
    Java中的比较器Comparable、Comparator
    Java创建线程的方法
    java日期格式化
    Docker容器如何修改hosts
    使用postman可以正常访问,但是在应用中返回415状态码
    使用tcpdump进行抓包
  • 原文地址:https://www.cnblogs.com/happen-/p/5411429.html
Copyright © 2011-2022 走看看