相关疑难解决方法(0)

如果表单在具有ng-if条件的div内,则无法读取未定义的属性'$ valid'

在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)

forms angularjs angularjs-validation

3
推荐指数
1
解决办法
3141
查看次数

标签 统计

angularjs ×1

angularjs-validation ×1

forms ×1