简体   繁体   English

ng-blur不会在父元素上触发

[英]ng-blur not firing on parent elements

I'm trying to use ng-blur on a <tr ng-blur="blurFn()"> but it doesn't fire. 我正在尝试在<tr ng-blur="blurFn()">上使用ng-blur,但它不会触发。 It fires only on the child <input> element. 它仅在子<input>元素上触发。

This is not specific to <tr> or <td> . 这不是<tr><td>特有的。 According to Angular documentation , ng-blur reacts to the blur event , which doesn't "bubble". 根据Angular 文档ng-blur会对模糊事件做出反应, 模糊事件不会“冒泡”。 "focusout" event bubbles, but there is no ng-focusout. “焦点”事件泡沫,但没有ng-focusout。

Am I missing something or do I need to create a new directive to handle focusout event? 我错过了什么或者我是否需要创建一个新的指令来处理焦点事件?

Here's the code snippet and fiddle : 这是代码片段和小提琴

html HTML

<div ng-app="App">
    <div ng-controller="Ctrl as ctrl">
        <table>
            <tr ng-repeat="item in ctrl.items" ng-blur="ctrl.blurFn()">
                <td><input ng-model="item.A"/></td>
                <td><input ng-model="item.B"/></td>
            </tr>
        </table>
    </div>
</div>

js JS

angular.module("App", [])
.controller("Ctrl", function(){
    this.blurFn = function($event){
        console.log($event.target);
    };

    this.items = [
        {A: "111", B: "222"},
        {A: "111", B: "222"},
        {A: "111", B: "222"},
        {A: "111", B: "222"},
        {A: "111", B: "222"}
        ];
});

One way would just be to create a focus out directive of your own. 一种方法就是创建自己的焦点指示。

 angular.module("App", []) .controller("Ctrl", function(){ this.blurFn = function($event){ console.log("Yay, blurred"); this.name = "focessed out at" + $event.target.name; }; this.items = [ {A: "111", B: "222"}, {A: "111", B: "222"}, {A: "111", B: "222"}, {A: "111", B: "222"}, {A: "111", B: "222"} ]; //This is just the way other handlers are defined... }).directive('focusout', ['$parse', function($parse) { return { compile: function($element, attr) { var fn = $parse(attr.focusout); return function handler(scope, element) { element.on('focusout', function(event) { scope.$apply(function() { fn(scope, {$event:event}); }); }); }; } }; }]); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js"></script> <div ng-app="App"> <div ng-controller="Ctrl as ctrl"> {{ctrl.name}} <table> <tr ng-repeat="item in ctrl.items" focusout="ctrl.blurFn($event)"> <td><input ng-attr-name="A{{$index}}" ng-model="item.A"/></td> <td><input ng-attr-name="B{{$index}}" ng-model="item.B"/></td> </tr> </table> </div> </div> 

You may want to be aware of the fact that, focusout event is not supported in firefox. 您可能想知道firefox中不支持focusout事件的事实。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM