2015-09-03 38 views
3

焦點事件,我有指令的輸入如何捕捉模糊或角指令

<input ng-model="inputModel" ng-disabled="ngDisabled || isLoading" value="{{value}}" type="{{type}}" placeholder="{{placeholder | translate}}"> 

我使用這個指令是這樣的:

<input-ext type="'text'" name="email" ng-model="registerCtrl.email" ng-blur="registerCtrl.test()" required></input-ext> 

我想模糊後,我的指令內部執行模糊在input-ext中,對於控制器中的這個示例代碼,如何做到這一點?

+0

向我們顯示該指令的JS。 –

+0

要觸發模糊,您需要設置焦點。你可能需要$超時設置重點。 – YOU

+0

檢查[this](http://stackoverflow.com/questions/14859266/input-autofocus-attribute)並最終[this](http://jsfiddle.net/HarshMehta/q2zcf5yp/)。 – road2victory

回答

14

在你的鏈接功能,您將它們綁定

link: function (scope, element, attrs) { 
     element.bind('blur', function (e) { 
      //do something 
     }); 
    } 
0
.directive('inputExt', function() { 
     return { 
      restrict: 'E', 
      templateUrl: 'templates/inputExt3.html', 
      require: 'ngModel', 
      scope: { 
       'name' : '@', 
       'type' : '=', 
       'placeholder' : '=', 
       'value' : '=', 
       'ngDisabled': '=', 
       'isLoading' : '=', 
       'customStatus': '=', 
       'cutomStatusType': '=', 
       'test' : '&' 
      }, 
      link: function($scope, element, attrs, ngModelCtrl) { 
       $scope.placeholder = $scope.placeholder == undefined ? $scope.name : $scope.placeholder; 
       $scope.$watch('inputModel', function() { 
        ngModelCtrl.$setViewValue($scope.inputModel); 
       }); 
       ngModelCtrl.$parsers.push(function(viewValue) { 
        ngModelCtrl.$validate(); 
        $scope.invalid = ngModelCtrl.$invalid; 
        $scope.error = ngModelCtrl.$error; 
        return viewValue; 
       }); 
      } 
     } 
    }) 

我不知道,你已瞭解我很好,如果用戶添加到inputExt NG模糊,並加入到這個atribiute一些參數,函數,這個函數應該在用戶輸入指令時執行