我在AngularJS中遇到表单问题。我需要进行自定义验证。但我无法访问表单或模板(或控制器)中的$error
对象以实现此目的。
我以前在其他项目中做过这个,它总是奏效。我设法发现,表格处于某种奇怪的状态。我试图在模板中显示其内容,但它不起作用。当我调用myForm表单并在HTML中的某处写{{myForm}}
时,我希望看到类似这样的内容:
{ “$错误”:{}, “$名”: “myForm会”, “$脏”:假的, “$原始”:真实的, “$有效”:真实的, “$无效”:假” $提交 “:假的,” 用户 “:{” $ viewValue “:” 约翰 多伊 “ ”$ modelValue“:” 约翰 多伊”, “$验证”:{}, “$ asyncValidators”:{}, “$解析器”:[], “$格式化”:[空], “$ viewChangeListeners”:[], “$不变”:真“$摸”:假的, “$原始”:真实的, “$脏”:假的, “$有效”:真实的, “$无效”:假的, “$错误”:{}, “$名”:”用户”, “$选项”:空}, “电子邮件”:{ “$ viewValue”: “john.doe@gmail.com”, “$ modelValue”: “john.doe@gmail.com”, “$验证” :{}, “$ asyncValidators”:{}, “$解析器”:[], “$格式化”:[空], “$ viewChangeListeners”:[], “$不变”:真实的, “$摸”:假的“$原始”:真实的, “$脏”:假的, “$有效”:真实的, “$无效”:假的, “$错误”:{}, “$名”: “电子邮件”, “$选项”日期null}}
然而,在目前的项目中,我看到了这一点:
{"user":{},"email":{}}
为什么会如此minified
?
这里是完整的源代码示例:
<!DOCTYPE html>
<html>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body>
<h2>Validation Examplea</h2>
<form ng-app="myApp" ng-controller="validateCtrl"
name="myForm" novalidate>
<p>Username:<br>
<input type="text" name="user" ng-model="user" required>
<span style="color:red" ng-show="myForm.user.$dirty && myForm.user.$invalid">
<span ng-show="myForm.user.$error.required">Username is required.</span>
</span>
</p>
<p>Email:<br>
<input type="email" name="email" ng-model="email" required>
<span style="color:red" ng-show="myForm.email.$dirty && myForm.email.$invalid">
<span ng-show="myForm.email.$error.required">Email is required.</span>
<span ng-show="myForm.email.$error.email">Invalid email address.</span>
</span>
</p>
<p>
<input type="submit"
ng-disabled="myForm.user.$dirty && myForm.user.$invalid ||
myForm.email.$dirty && myForm.email.$invalid">
</p>
{{myForm}}
</form>
<script>
var app = angular.module('myApp', []);
app.controller('validateCtrl', function($scope) {
$scope.user = 'John Doe';
$scope.email = 'john.doe@gmail.com';
});
</script>
</body>
</html>
在我的代码中,我看到了:
{"user":{},"email":{}}
在在线编辑器中,我看到了:
{"$error":{},"$name":"myForm","$dirty":false,"$pristine":true,"$valid":true,"$invalid":false,"$submitted":false,"user":{"$viewValue":"John Doe","$modelValue":"John Doe","$validators":{},"$asyncValidators":{},"$parsers":[],"$formatters":[null],"$viewChangeListeners":[],"$untouched":true,"$touched":false,"$pristine":true,"$dirty":false,"$valid":true,"$invalid":false,"$error":{},"$name":"user","$options":null},"email":{"$viewValue":"john.doe@gmail.com","$modelValue":"john.doe@gmail.com","$validators":{},"$asyncValidators":{},"$parsers":[],"$formatters":[null],"$viewChangeListeners":[],"$untouched":true,"$touched":false,"$pristine":true,"$dirty":false,"$valid":true,"$invalid":false,"$error":{},"$name":"email","$options":null}}
示例来自此网站:http://www.w3schools.com/angular/angular_validation.asp