我有一个2表单输入(firstName,lastName),我想通过查询我的数据库来检查它们是否是唯一的(表单验证)。所以我基本上想要在输入两个输入元素时进行回调。
这样做的角度方式是什么?
我正在考虑获取元素并在检查其他输入是否为.on('blur')
时使用valid
,但这感觉非常像jQuery。
答案 0 :(得分:0)
在输入上使用ng-blur。 这是一个例子:
HTML:
<div ng-app='myApp' ng-controller='myCtrl'>
Username : <input type='text' ng-model='model.username' ng-blur='checkInputs()' /><br/>
Password: <input type='text' ng-model='model.password' ng-blur='checkInputs()' /> <br/>
</div>
JS:
var app = angular.module('myApp',[]);
app.controller('myCtrl', function($scope){
$scope.model = {username:'', password:''};
$scope.checkInputs = function(){
if($scope.model.username != '' && $scope.model.password != ''){
if($scope.model.username == 'testuser' || $scope.model.password == 'testpass')
{
alert('insert unique fields!!!!')
}
}
}
});
在这种情况下,func将位于您的控制器中,并且可以访问用户名\密码。你需要执行一个ajax来检查这个,但我认为你明白了这一点......
这是Fiddle。