【问题标题】:ng-blur not firing on parent elementsng-blur 不在父元素上触发
【发布时间】:2014-11-15 05:00:00
【问题描述】:

我正在尝试在 <tr ng-blur="blurFn()"> 上使用 ng-blur,但它不会触发。它仅在子 <input> 元素上触发。

这并不特定于 <tr> 或 <td>。根据 Angular documentation 的说法,ng-blur 对 blur event 做出反应,这不会“冒泡”。 "focusout" 事件冒泡,但没有 ng-focusout。

我是否遗漏了什么或者我需要创建一个新指令来处理焦点事件?

这里是代码 sn-p 和 fiddle:

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

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"}
        ];
});

【问题讨论】:

    标签: javascript html angularjs dom angularjs-directive


    【解决方案1】:

    一种方法是创建自己的焦点输出指令。

    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>

    您可能想知道,Firefox 不支持 focusout 事件。

    【讨论】:

    • 感谢您的示例。有没有好办法支持这种跨浏览器?
    • 我刚刚意识到我的问题还没有结束。即使我离开 但留在 内,我也会得到 focusout 事件
    • @NewDev 这就是焦点输出,对,你希望它们冒泡,所以即使你留下任何输入显然它们也会冒泡(并且对于 tr 没有焦点输出/模糊,它是针对表单元素而不是针对html中的每个元素),现在它完全违背了你的问题。
    • @NewDev 您可能需要重新考虑您正在尝试做什么> 您到底想做什么?可能会有更好的解决方案。
    • 同意。所提问题已得到解答。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签