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 ();  
          
        });  
  • 相关阅读:
    Srt字幕文件解析
    有意思的一些处理
    CMSampleBufferRef转换
    不知为什么的警告和报错
    X Postgres copy命令导入导出数据
    X Oracle打Patch报错Missing command :fuser
    X wal_segment_size参数的理解与调优
    X PostgreSQL 11、12 开启归档日志
    X 手动安装postgresql扩展插件
    P1501 [国家集训队]Tree II
  • 原文地址:https://www.cnblogs.com/happen-/p/5411429.html
Copyright © 2011-2022 走看看