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 ();  
          
        });  
  • 相关阅读:
    mySQL如何在查询的结果前添加序号
    bootstrap 列表前添加序号 1.10版本
    sql 如何优先显示不为空的字段 并进行排序
    java面向对象总结
    线程总结(二)
    数据库索引介绍(转载)
    线程总结(一)
    GUI图形界面编程之事件处理机制
    Eclipse快捷键大全(转载)
    JDBC数据库编程总结
  • 原文地址:https://www.cnblogs.com/happen-/p/5411429.html
Copyright © 2011-2022 走看看