莫回无
如果我想实现这样一个功能,当一个input失去光标焦点时(blur),执行一些语句,比如当输入用户名后,向后台发ajax请求查询用户名是否已经存在,好有及时的页面相应。输入 camnprangularjs directive input focus失去焦点后提示 camnpr 这个用户名已经存在angularjs directive input focus用户名已经存在HTML代码如下:<body ng-controller="MainCtrl"><lable>用户名:<input type="text" ng-model="username" ng-blur="checkUsername()" /><span style="color:red;" ng-show="usernameAlreadyExist">用户名已经存在</span></lable></body>controller和directive的定义app.controller('MainCtrl', function($scope) {$scope.checkUsername = function() {//send ajax to check on serverif ($scope.username === 'hellobug') {$scope.usernameAlreadyExist = true;}}});app.directive('ngBlur', function($document) {return {link: function(scope, element, attrs) {$(element).bind('blur', function(e){scope.$apply(attrs.ngBlur);});}}})