无法读取未定义的属性“$valid" [英] Cannot read property '$valid' of undefined
问题描述
我有一个带有名称字段和两个验证的表单 - required 和 max-length.当我输入正确的值时,控制器中的表单显示有效并返回 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 这里
提前致谢
推荐答案
在表单中添加 ng-submit
属性:
Add ng-submit
attribute to the form:
<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"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!