javascript - 类型错误 : Cannot read property '$valid' of undefined on using ng-submit

标签 javascript angularjs forms validation

我提到了Angular JS Form validation inside tabset give error : TypeError: Cannot read property '$valid' of undefined但这并不正是我所寻找的。

我有这个 html 代码,

<form name="userForm" ng-submit="submit()" novalidate>
    <input type="text" class="form-control" ng-model="user.name" required>
    <button type="submit" class="btn btn-primary" ng-disabled="!userForm.$valid">Submit</button>
</form>

我的 js 是这样的,

$scope.submitForm = function () {

    // check to make sure the form is completely valid
    if ($scope.userForm.$valid) {
        alert('its working');
    }
};

我收到此错误。使用 ng-submit 时有办法解决这个问题吗?

最佳答案

您可以尝试一下下面的代码吗:

$scope.submitForm = function() {

    // check to make sure the form is completely valid
    if (userForm.$valid) {
        alert('its working');
    }

};

我从 $scope.userForm.$valid 中删除了 $scope,因为 userForm 不是 $scope 的属性。

你可以查看AngularJS官方文档,especially this part ,您可以在其中查看它们如何绑定(bind)到表单。

关于javascript - 类型错误 : Cannot read property '$valid' of undefined on using ng-submit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38301366/

相关文章:

javascript - javascript在验证后提交表单

javascript - Google 脚本 - 表单 - 删除分页符/节时出现问题 - "Invalid data updating form"

javascript - 无法使用 d3.js 在 svg 中绘制圆 Angular 矩形

javascript - 如何使用原始 Canvas 的上下文和数据更新克隆的 HTML Canvas 元素?

javascript - 动态加载表格主体内容,无需更改表格标题

javascript - 如何使用表单在 View 中更新模型列表而不使用 rails 中的 javascript (coffeescript) 刷新?

javascript - 在 Javascript 中创建输入掩码有任何指南吗?

angularjs - WKHTMLTOPDF和Angular

javascript - Angular 未捕获对象错误

javascript - 使用 ng-repeat AngularJS 对数组中的项目进行计数