简体   繁体   English

输入字段为ng-repeat时,ng-disabled无法正常工作

[英]ng-disabled not working when input fields are in an ng-repeat

I'm trying to make my submit button disabled if the input fields from an array are invalid. 如果数组中的输入字段无效,我试图将我的提交按钮禁用。

Here's my html 这是我的HTML

<form name="membersForm">
  <div ng-repeat="emailInput in emailInputs">
    <div class="input-field col s10">
      <input id="email{{$index}}" type="email" class="validate email" length="50" maxlength="50" ng-model="email['email_' + $index]" required>
      <label for="email{{$index}}">{{emailInput.label}}</label>
    </div>
    <div class="input-field col s2">
      <div class="btn btn-floating waves-effect waves-light red" ng-click="removeEmail($index)">
        <i class="material-icons">clear</i>
      </div>
    </div>
  </div>
  <div class="col s12">
    <a class="btn-floating btn waves-effect waves-light" id="addEmail" ng-click="addEmail()">
      <i class="material-icons">add</i>
    </a>
  </div>
  <a class="waves-effect waves-light btn" type="submit" ng-disabled="membersForm.$invalid">
    Continue
  </a>
</form>

as you can see, I have a button to add more email inputs dynamically. 如您所见,我有一个按钮可以动态添加更多电子邮件输入。

Here's my controller: 这是我的控制器:

class teamMembersCtrl {
  constructor($scope) {
    'ngInject';

    $scope.emailInputs = [
      {
        label: 'Email'
      }
    ];

    $scope.email = {};

    $scope.setCurrentPrice();

    $scope.addEmail = function () {
      $scope.emailInputs.push({
        label: 'Email'
      });
    }

    $scope.removeEmail = function ($index) {
      $scope.emailInputs.splice($index,1);
    }
  }
}

QUESTIONS 问题

How can I make the submit button disabled if there is an email input that's invalid where email inputs are added dynamically with an ng-repeat? 如果使用ng-repeat动态添加电子邮件输入时,如果电子邮件输入无效,如何禁用提交按钮?

Thanks! 谢谢!

from the angularjs docs for forms : 从angularjs文档获取表单

A form is an instance of FormController. 表单是FormController的实例。 The form instance can optionally be published into the scope using the name attribute. 可以选择使用name属性将表单实例发布到范围中。

Similarly, an input control that has the ngModel directive holds an instance of NgModelController. 同样,具有ngModel指令的输入控件包含NgModelController的实例。 Such a control instance can be published as a property of the form instance using the name attribute on the input control. 可以使用输入控件上的name属性将该控件实例发布为表单实例的属性。 The name attribute specifies the name of the property on the form instance. name属性指定表单实例上属性的名称。

This implies that the internal state of both the form and the control is available for binding in the view using the standard binding primitives. 这意味着表单和控件的内部状态都可用于使用标准绑定原语在视图中进行绑定。

What this means is that if you add the name attribute to your form then you get access to the form and all it's properties in your scope, this means that you're given access to all the information you need for validating your form including if it's valid or not, pristine or dirty etc. In order for this to work you need 2 main things: 这意味着,如果将name属性添加到表单中,则可以访问该表单及其作用域中的所有属性,这意味着您将获得验证表单所需的所有信息,包括有效或无效,原始或肮脏等。要使此功能正常运行,您需要做两件事:

  1. Add name attribute to your form, this will be the name of the variable to get the form data. 在表单中添加名称属性,这将是获取表单数据的变量的名称。 So name = "myform" stores the form in $scope.myform . 所以name = "myform"将表单存储在$scope.myform
  2. Add ng-model to all your inputs, if an input doesn't have an ng-model then it won't be considered in the form's validation. ng-model添加到所有输入中,如果输入没有ng-model,则在表单的验证中将不考虑它。

After that you can always find out if your form is valid using $scope.myform.$valid . 之后,您始终可以使用$scope.myform.$valid确定表单是否$scope.myform.$valid As an added bonus you could also add the name property to each input, this will also add an object for each input inside the $scope.myform containing all the information for that input and it's validation. 作为额外的好处,您还可以向每个输入添加name属性,这还将在$scope.myform中为每个输入添加一个对象,其中包含该输入及其验证的所有信息。

EDIT: Heres a plunkr with an example of what you want to do: http://plnkr.co/edit/86p9PrNnFVdfB7y406a6?p=preview 编辑:这里有一个您想要做的事的plunkr: http : //plnkr.co/edit/86p9PrNnFVdfB7y406a6?p=preview

To disable the submit button set $scope.invalid = true; 要禁用提交按钮,请设置$ scope.invalid = true;。 in your controller and than in your html ng-disabled="invalid". 在您的控制器中,而不是在html ng-disabled =“ invalid”中。 Depending on how you check if an email input is invalid, set the $scope.invalid accordingly. 根据检查电子邮件输入是否无效的方式,相应地设置$ scope.invalid。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM