9.11 모니터링 워치

1803 단어

자체 가진angular 속성ng-model 감청

{{age}}
 angular.module('zfMod', [])
    .controller('WatchCtrl',function($scope){
        $scope.age = 100;
       $scope.$watch('age',function(newVal, oldVal){
            console.log(newVal, oldVal);
      })

        console.log($scope);
    })

스스로 데이터 귀속을 실현하다

{{age}}
 angular.module('zfMod', [])
    .controller('WatchCtrl',function($scope,$interval){
        $scope.age = 100;
        $interval(function(){
                $scope.age = parseInt($scope.age)+1;
        },1000);

        console.log($scope);
    }).directive('zfModel',function($timeout){
        return {
            link:function(scope,element,attrs){
                element.on('keypress',function(){
                    console.log('keypress',element.val());
                    // , , {{age}} 
                    scope.$apply(function(){
                        scope.age = element.val();
                    });
                    /*$timeout(function(){
                        scope.age = element.val();
                    },0);*/

                });
                // , 
                var watcher = scope.$watch('age',function(newVal,oldVal){
                    console.log(newVal,oldVal);
                    if(newVal != oldVal){
                        element.val(scope.age);
                    }
                })
                console.log('scope',scope);
            }
        }
    })

좋은 웹페이지 즐겨찾기