【发布时间】:2014-07-09 16:31:25
【问题描述】:
我一直在使用 AngularJS。我有以下表格:
<html lang="en" ng-app="app">
...
<div ng-controller="RegisterController">
<h3>Register</h3>
<form name="registerForm" class="form-horizontal panel-body" novalidate="novalidate">
<form name="registerForm" class="form-horizontal panel-body" novalidate="novalidate">
<div class="form-group">
<label name="firstname" class="col-md-2 control-label">First Name</label>
<div class="col-md-4">
<input type="text" class="form-control" name="firstname" ng-model="user.firstname" placeholder="First Name" required>
</div>
</div>
<div class="form-group">
<label name="lastname" class="col-md-2 control-label">Last Name</label>
<div class="col-md-4">
<input type="text" class="form-control" name="lastname" ng-model="user.lastname" placeholder="Last Name" required>
</div>
</div>
<div class="form-group">
<label name="email" class="col-md-2 control-label">Your email</label>
<div class="col-md-4">
<input type="email" class="form-control" name="email" ng-model="user.email" placeholder="Email Address" required>
</div>
<div class="col-md-2">
<span ng-show="showError(userInfoForm.email, 'email')" ...>
You must enter a valid email
</span>
<span ng-show="showError(userInfoForm.email, 'required')" ...>
This field is required
</span>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-6">
<button class="btn btn-sm btn-primary" ng-disabled="!canRegister()" ng-click="submit()">Register</button>
</div>
</div>
我还有 app.js 文件,其中包含配置和控制器代码,我只是粘贴控制器代码:
app.controller('RegisterController', function($scope) {
$scope.getCssClasses = function(ngModelContoller) {
return {
error: ngModelContoller.$invalid && ngModelContoller.$dirty,
success: ngModelContoller.$valid && ngModelContoller.$dirty
};
};
$scope.showError = function(ngModelController, error) {
return ngModelController.$error[error];
};
$scope.canRegister = function() {
return $scope.registerForm.$dirty &&
$scope.registerForm.$valid;
};
});
我的问题是,当我加载注册页面(包含表单的页面)时,我打印了两个错误: 您必须输入有效的电子邮件 此字段为必填项 我想知道当输入脏且无效时如何打印错误,当输入文本有效时如何消失。 感谢您的帮助
【问题讨论】:
标签: angularjs validation angular-ui-bootstrap