3 回答

TA貢獻1865條經驗 獲得超7個贊
如果我想實現這樣一個功能,當一個input失去光標焦點時(blur),執行一些語句,比如當輸入用戶名后,向后臺發ajax請求查詢用戶名是否已經存在,好有及時的頁面相應。
輸入 camnpr
angularjs 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 server
if ($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);
});
}
}
})
- 3 回答
- 0 關注
- 784 瀏覽
添加回答
舉報