New*_*Dev 5 html javascript dom angularjs angularjs-directive
我正在尝试使用ng-blur <tr ng-blur="blurFn()">但它不会触发.它仅在子<input>元素上触发.
这不是特定的<tr>或<td>.根据Angular 文档,ng-blur对模糊事件做出反应,模糊事件不会"冒泡"."焦点"事件泡沫,但没有ng-focusout.
我错过了什么或者我是否需要创建一个新的指令来处理焦点事件?
这是代码片段和小提琴:
<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>
Run Code Online (Sandbox Code Playgroud)
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"}
];
});
Run Code Online (Sandbox Code Playgroud)
一种方法是创建自己的焦点指令。
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});
});
});
};
}
};
}]);Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
您可能想知道,Firefox 不支持 focusout 事件。
| 归档时间: |
|
| 查看次数: |
3932 次 |
| 最近记录: |