2013-10-18 95 views

回答

25

release 1.2.0rc1开始有一个ng-blur指令

的jsfiddle:http://jsfiddle.net/9mvt8/6/

HTML:

<div ng-controller="MyCtrl"> 
    <input type='text' ng-blur='blurCount = blurCount + 1'/> 

    <input type='text' ng-blur='blurCount = blurCount + 1' /> 

    blur count: {{blurCount}} 
</div> 

脚本:

function MyCtrl($scope) { 
    $scope.blurCount = 0; 
    $scope.name = 'Superhero'; 
} 
+2

这不是我想要的东西,如果我没有改变的价值,但失去的那场焦点,它仍然会触发功能 – 9blue

+2

你会处理你自己在模糊处理程序中。如果价值变化做你所需要的,否则什么也不做。 – Jason

+6

这个官方的将防止需要处理模糊处理程序中的更改逻辑:

1

如果使用NG-模糊,然后(正如Jason所说)使用h安德勒检查输入类=“ng-dirty”,它会告诉你输入值是否已经改变。

+0

ng-dirty用于整个表单,而不仅仅是一个输入框。 – dman

61

使用ng-model-options

$scope.onchange = function() {} 
<input type="text" ng-model="x" ng-change="onchange()" ng-model-options="{updateOn: 'blur'}"/> 
+7

这应该是被接受的答案。 –

+0

这是答案,谢谢 – ctorx