本文介绍了都是myForm.$ valid&myForm.$ invalid在角度形式上未定义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 myform &的表单.我正在尝试使用以下代码设置 ng-disabled :

I have a form named myform & I'm trying to set ng-disabled with this code:

ng-disabled="myForm.$invalid"

但同时 myForm.$ invalid 和& myForm.$ valid 未定义.到底是什么问题?我在控制台& myForm 已正确设置为表单.

but both myForm.$invalid & myForm.$valid are undefined. What is the issue exactly? I checked in console & myForm is correctly set to the form.

更新

 <form id="commissionForm" name="myForm" class="form-horizontal">

            <div class="form-group">
                <div class="col-sm-8 col-md-8 col-lg-8 col-xs-8" />
                <div class="col-sm-2 col-md-2 col-lg-2 col-xs-2" >
                    <button name="NextBtn" id="NextBtn"
                            ng-class="{disabled:commissionForm.$invalid}"
                            ng-disabled="myForm.$invalid"
                            ng-click="nextBtnClicked()" class="btn btn-primary"
                            >Next</button>
                </div>
            </div>
 </form>

推荐答案

您需要将'myForm'更改为'commisionForm'以使其正常运行.另外,您的表单需要使用 ng-model 至少具有一个绑定到模型的元素.否则,验证将不会触发.

You need to change 'myForm' to 'commisionForm' to make it work. Also, your form needs to have at least one element that binds to the model, using ng-model. Otherwise, validation will not fire.

工作代码示例:

var app = angular.module('myApp', []);
app.controller('myController', function($scope) {

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myController">
  <form id="commissionForm" name="commissionForm" class="form-horizontal">
    <div>form $valid: {{commissionForm.$valid}}</div>
    <div>form $invalid: {{commissionForm.$invalid}}</div>
    <div>An input box with max length 5, to make the form invalid:</div>
    <input ng-maxlength="5" ng-model="somemodel"/>
    <div class="form-group">
      <div class="col-sm-8 col-md-8 col-lg-8 col-xs-8" />
      <div class="col-sm-2 col-md-2 col-lg-2 col-xs-2">
        <button name="NextBtn" id="NextBtn" ng-class="{disabled:commissionForm.$invalid}" ng-disabled="commissionForm.$invalid" ng-click="nextBtnClicked()" class="btn btn-primary">Next</button>
      </div>
    </div>
  </form>
</div>

这篇关于都是myForm.$ valid&amp;myForm.$ invalid在角度形式上未定义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 04:36