javascript 如何在 angularJS 指令中捕捉模糊或焦点事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32371291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to catch blur or focus event in angularJS directive
提问by Artur Kasperek
I have directive with input
我有输入指令
<input ng-model="inputModel" ng-disabled="ngDisabled || isLoading" value="{{value}}" type="{{type}}" placeholder="{{placeholder | translate}}">
I use this directive like this:
我像这样使用这个指令:
<input-ext type="'text'" name="email" ng-model="registerCtrl.email" ng-blur="registerCtrl.test()" required></input-ext>
I want to after blur inside my directive to executed blur in input-ext ... , for this example code in controller, how to make this ?
我想在我的指令中执行模糊之后,在 input-ext ... 中执行模糊,对于控制器中的这个示例代码,如何做到这一点?
回答by Ahmet Zeytindal?
On your link function you bind them
在您的链接功能上,您将它们绑定
link: function (scope, element, attrs) {
element.bind('blur', function (e) {
//do something
});
}
回答by Artur Kasperek
.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;
});
}
}
})
I dont known that You undestand me well, if user add to inputExt ng-blur and add to this atribiute some argument, function, this function should be executed when user go out from input in directive
我不知道你理解我很好,如果用户添加到 inputExt ng-blur 并添加到这个属性一些参数,函数,当用户从指令中的输入中退出时应该执行这个函数