gpt4 book ai didi

javascript - ng-blur 不触发父元素

转载 作者:太空狗 更新时间:2023-10-29 16:06:45 27 4
gpt4 key购买 nike

我正在尝试在 <tr ng-blur="blurFn()"> 上使用 ng-blur但它不火。它只在 child 身上触发 <input>元素。

这不是特定于 <tr><td> .根据 Angular documentation , ng-blurblur event 使用react,它不会“冒泡”。 “focusout”事件冒泡,但没有 ng-focusout。

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

这是代码片段和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"}
];
});

最佳答案

一种方法就是创建您自己的聚焦指令。

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 事件。

关于javascript - ng-blur 不触发父元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25953567/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com