我有一个使用 ng-if 在 div 内形成表单健康)状况。最初表单是关闭的。单击按钮后,将显示表单。但在提交表格时,我得到Cannot read property '$valid' of undefined
错误。如果我更改ngif和ng-show,效果完美。但我必须使用 ng-if 。我找不到错误原因。还有一件事,在出现错误时,我也得到了表单对象为空。如果有人知道答案,将不胜感激。
这是我的代码
HTML
<div id="addCommentDiv" ng-if="showAddCommentForm == true">
<form id="formAddComment" name="formAddComment" novalidate>
<textarea id="taAddComment" name="taAddComment" placeholder="Add a comment.." ng-model="new_comment" ng-maxlength="1000" ng-required="true"></textarea>
<button type="button" ng-click="addComment()" ng-disabled="addCommentDisabled">Ok</button>
</form>
</div>
JS
$scope.addCommentDisabled = false;
$scope.addComment = function () {
if ($scope.formAddComment.$valid) {
console.log('valid');
}
}
在 addComment 中传递表单 -fiddle https://jsfiddle.net/c5vxffLa/
$scope.addComment = function (formAddComment) {
console.log('valid');
if (formAddComment.$valid) {
console.log('valid');
}
}
<button type="button" ng-click="addComment(formAddComment)" ng-disabled="addCommentDisabled">Ok</button>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)