2017-06-29 67 views
1

我想觀看應用程序中所有input焦點活動。AngularJS觀看焦點所有輸入

事情是這樣的:

$scope.$watch('input',function(e){ 
    if(e.focus){ 
    // do stuff 
    } 
}); 

任何想法,這可怎麼辦呢?

+0

你設置監視輸入元素或範圍變量? – HARI

回答

2

你不注意事件,你聽它。另外,搞亂DOM也是一個指令的任務。因此,這個答案使用了指示方法。你可以發出一個事件或者在內部完成所有的「東西」。

angular.module('app', []) 
 
    .directive('input', function() { 
 
    return { 
 
     restrict: 'E', 
 
     link: Link 
 
    }; 
 

 
    function Link(scope, elem, attrs) { 
 
     elem.on('focus', function() { 
 
     // do stuff 
 
     console.log(elem[0].name + ' has been focused!'); 
 
     }); 
 
    } 
 
    });
<div ng-app="app"> 
 
    <label for="input1">input1</label> 
 
    <input name="input1" type="text"> 
 
    <label for="input2">input2</label> 
 
    <input name="input2" type="text"> 
 
</div> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

+0

完美!這正是我所期待的。 – Matt