本文主要介绍了angularjs实现的获取焦点及失去焦点时的表单验证功能,涉及angularjs使用ng-blur、ng-focus针对表单事件监听相关操作技巧,需要的朋友可以参考下,希望能帮助到大家。
<!DOCTYPE html>
<html ng-app="formExample">
<head>
<meta charset="UTF-8">
<title></title>
<script src="../js/angular.js"></script>
<script>
angular.module('formExample', [])
.controller('FormController', ['$scope', function($scope)
{
$scope.userType = 'guest';
$scope.change = false;
}]);
</script>
</head>
<body>
<form name="myForm" ng-controller="FormController">
userType: <input name="input" ng-model="userType" ng-blur="change=true" ng-focus="change=false" required>
<span class="error" ng-show="myForm.input.$error.required && change">必填项</span><br>
</form>
</body>
</html>运行效果:

相关推荐:
完美解决BootStrap模态框和select2合用时input无法获取焦点问题










