我在div中有一个带有ng-if条件的表单.最初表格已关闭.单击按钮,将显示表单.但在提交表格时,我收到了Cannot read property '$valid' of undefined
错误.如果我将ng-if更改为ng-show,它将完美无缺.但我必须使用ng-if.我找不到错误的原因.还有一件事,在获取错误时,我也将表单对象变为null.如果有人知道答案,我们将不胜感激.
这是我的代码
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>
Run Code Online (Sandbox Code Playgroud)
JS
$scope.addCommentDisabled = false;
$scope.addComment = function () {
if ($scope.formAddComment.$valid) {
console.log('valid');
}
}
Run Code Online (Sandbox Code Playgroud)