问题描述
我有一个带有名称字段和两个验证的表单-必需和最大长度.当我输入正确的值时,控制器中的表格显示有效并返回true.但是在输入错误的数据时,$ valid不会抛出错误,只是说--
I have a form with a name field and two validations - required and max-length.When I enter correct value, the form in controller is showing valid and returning true. But on entering wrong data, $valid is not throwing false and simply says -
Cannot read property '$valid' of undefined
为什么在这种情况下表单变得不确定?
Why is the form getting undefined in this case?
Jsfiddle 此处
Jsfiddle here
预先感谢
推荐答案
在表单中添加ng-submit
属性:
<form name="myForm" ng-submit="SaveAndNext()" novalidate>
将控制器更改为:
function myCtrl($scope, $log){
$scope.data = {};
$scope.SaveAndNext = function(){
if($scope.myForm.$valid){
$log.info('Form is valid');
$log.info($scope.data);
} else {
$log.error('Form is not valid');
}
}
}
从提交"按钮中删除ng-click
事件处理程序
Remove ng-click
event handler from your submit button
更新的提琴: http://jsfiddle.net/9cgopo7d/1/
我包括了$log
服务,因为它是有用的默认服务,您可能对此一无所知.
I included the $log
service because it's a useful default service and you might not know about it.
这篇关于无法读取未定义的属性"$ valid"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!