2016-08-01 24 views
1

我想在div中包含html输入元素radio和复选框的div上添加ng-blur(onFocusOut事件)。因为在数组(样本)中持有我的值的ng模型所以我想从div中注意不是从每个输入元素包含div的情况下触发事件。关注div外,我将有$ http服务在服务器上保存模型。

<div ng-app="app"> 
     <div ng-controller="focusCtrl"> 
      <div ng-blur="sentHttp()" style="height:100px;width:100px;background-color:blue;"> 
       <input type="radio" name="radio" ng-model="sample[shrk]" />A 
       <input type="radio" name="radio" ng-model="sample[shrk]" />b 

     <input type="checkbox" ng-model="sample[shrk]" />1 
     <input type="checkbox" ng-model="sample[shrk]" />2 
     <input type="checkbox" ng-model="sample[shrk]" />3 

      </div> 
     </div> 
     </div> 

角控制器

angular.module('app').controller('focusCtrl', ['$scope', function ($scope) { 
    $scope.sample= {}; 
    $scope.sentHttp = function() { 
     console.log("I'm called!"); 
     }; 
}]); 

预先感谢。

回答

0

我相信NG-模糊内部实现onfocusout事件,因为DIV不火的onfocusoutng-blur不会在这种情况下工作

你要听click事件对身体和检查的目标是不是div,然后执行一些

1

正如有人说的div不火的焦点/模糊的事件,但是,使用简单的技巧,你可以实现它...

解决方案驻留在tabindex attri弼... https://www.barryvan.com.au/2009/01/onfocus-and-onblur-for-divs-in-fx/

它的工作原理,当点击股利之外,但与标签...你需要在这个片段中更多的工作...

function TestCtrl($scope) { 
 
    $scope.fields = { 
 
    "firstname": "Giuseppe", 
 
    "surname": "Mandato" 
 
    }; 
 
    
 
    $scope.sendHttp = function() { 
 
    console.log("fields", $scope.fields); 
 
    }; 
 
} 
 

 
angular 
 
    .module('test', []) 
 
    .controller("TestCtrl", ["$scope", TestCtrl]) 
 
;
.test { 
 
    padding: .5em 1em; 
 
    background: lightseagreen; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 

 
<section ng-app="test"> 
 
    <article ng-controller="TestCtrl"> 
 
    
 
    <div ng-blur="sendHttp($event)" tabindex="-1" class="test"> 
 
     <input type="text" ng-model="fields.firstname" /> 
 
     <input type="text" ng-model="fields.surname" /> 
 
    </div> 
 
    
 
    </article> 
 
</section>