-2
我有一个输入文本框,我希望当用户在文本框中输入值并失去焦点。我想从api值验证这个值,并且需要在输入框下面显示相应的消息。我该如何做角js如何在角度js中指定模糊事件
我有一个输入文本框,我希望当用户在文本框中输入值并失去焦点。我想从api值验证这个值,并且需要在输入框下面显示相应的消息。我该如何做角js如何在角度js中指定模糊事件
您可以使用ng-blur
指令。
<input type="text" ng-blur="ValidateBlur()" />
https://plnkr.co/edit/hy2sCVjvVqBrm1WSdiBl?p=preview
模块/控制器设置代码:
var app = angular.module("testApp", []);
app.controller("DarrenController", function($scope) {
$scope.Text = "Testing...";
$scope.ValidateBlur = function() {
alert('Validating');
}
});
HTML:
<body ng-app="testApp">
<div ng-controller="DarrenController">
<input type="text" ng-blur="ValidateBlur()" />
</div>
</body>
HTML
<input type="text" ng-model="search" placeholder="Search ... " ng-focus="focusMsg()" ng-blur="blurMsg()">
//控制器
$scope.focusMsg = function() {
console.log('Focus');
}
$scope.blurMsg = function() {
console.log('Blur');
}
看起来不错,但我怎么能显示验证自定义消息一样,如果用户类型的东西,然后如果他删除,并失去焦点,我需要证明验证MESSAGE-场空。如果他输入的值与db值中的值不匹配,那么我需要显示 - “该值在数据库中不存在”。 – user3395635
@ user3395635 - angular有一个$ http服务,所以如果你的API是REST,你可以使用这个服务来连接并返回数据库值并将它们传递给验证方法。 https://docs.angularjs.org/api/ng/service/$http –