如果div中的任何输入具有焦点,则angular指令突出显示

时间:2016-08-20 00:46:30

标签: javascript jquery css angularjs html5

我为带有表单元素的可重复部分创建了一个angular指令 我希望当div中的任何输入字段都处于焦点时,整个section / div会突出显示

template.html

<div class="col-md-12 employee-section">
<label for="name_{{$index}}">Name</label>
<input type="text" id="name_{{$index}}" class="col-md-6" ng-model="model.name"/>

<label for="address_{{$index}}">Address</label>
<input type="text" id="address_{{$index}}" class="col-md-6" ng-model="model.address"/>
</div>

指令

angular.module('test').directive('employee' , function(){
       return {
            link: function(scope, element){
            },
            restrict: 'AE',
            scope: {
                model: "="
            },
            templateUrl: 'template.html'
        };
 }

控制器

angular.module('test').controller('employeeCtrl' , function($scope){
    $scope.employees = [{name:'Jackk',address:'Main st'}, {name:'Jill',address:'Main st 123'}
});

html页面

<div ng-repeat="employee in employees>
    <employee model="employee"></employee>
</div>

1 个答案:

答案 0 :(得分:0)

这是我正在寻找的指令Plnkr

的链接
app.directive('ngFocusModel', function () {
    return function (scope, element) {

      var focusListener = function () {
          scope.hasFocus = true;
          scope.$digest();
      };

      var blurListener = function () {
          scope.hasFocus = false;
          scope.$digest();
      };


      element[0].addEventListener('focus', focusListener, true);
      element[0].addEventListener('blur', blurListener, true);
   };
});