ng-form inside ng-repeat with submit button outside of ng-repeat

笑着哭i 提交于 2019-12-10 19:24:15

问题


I've this code:

 <div class="row" ng-repeat="input in inputs | filter:inputNumber">
     <ng-form name="form1">
       <div class="col-xs-3">
            <div class="form-group">
                 <input ng-model="inputs.number[$index]" type="number" name="number$index" class="form-control" placeholder="" min="1" max="9999" required />
                     <small class="label label-danger" data-ng-show="submitted && form1.number$index.$error.required">required</small>
                     <small class="label label-danger" data-ng-show="submitted && form1.number$index.$error.number">number</small>
                     <small class="label label-danger" data-ng-show="submitted && form1.number$index.$error.max">number max</small>
            </div>
        </div>
      </ng-form>
  </div>

 <button data-ng-click="add(form1)" class="btn-add" style="padding-left: 40%;"></button>

Controller:

// Form add handler.
$scope.add = function(form) {
    // Trigger validation flag.
    $scope.submitted = true;

    // If form is invalid, return and let AngularJS show validation errors.
    if (form.$invalid) {
        console.log('invalid');
        return;
    } else {
        var newItemNo = $scope.inputs.length + 1;
        var inputs = { 'id': 'input' + newItemNo }
        $scope.inputs.push(inputs);
        $scope.isDisabled = false;
    }
};

The problem is I can only validate the form inputs inside ng-repeat and ng-form, and I need to trigger the event outside of that html block, maybe I'm just being noob (first time using angular1 validations). I will appreciate some help. Thanks in advance.


回答1:


Based on you comment and following the same goal of generating a form for each element in your ng-repeat, this should be the answer:

html

<form name="generalForm"> <!-- this form will be valid if all sub-forms are valid-->
 <div class="row" ng-repeat="input in inputs | filter:inputNumber">
     <ng-form name="form1">
       <div class="col-xs-3">
            <div class="form-group">
                 <input ng-model="inputs.number[$index]" type="number" name="number$index" class="form-control" placeholder="" min="1" max="9999" required />
                     <small class="label label-danger" data-ng-show="submitted && form1.number$index.$error.required">required</small>
                     <small class="label label-danger" data-ng-show="submitted && form1.number$index.$error.number">number</small>
                     <small class="label label-danger" data-ng-show="submitted && form1.number$index.$error.max">number max</small>
            </div>
        </div>
      </ng-form>
  </div>

 <button data-ng-click="add(generalForm)" class="btn-add" style="padding-left: 40%;"></button>
</form>

controller

// Form add handler.
$scope.add = function(form) {
    // Trigger validation flag.
    $scope.submitted = true;

    // If form is invalid, return and let AngularJS show validation errors.
    if (form.$invalid) {
        console.log('invalid');
        return;
    } else {
        var newItemNo = $scope.inputs.length + 1;
        var inputs = { 'id': 'input' + newItemNo }
        $scope.inputs.push(inputs);
        $scope.isDisabled = false;
    }
};


来源:https://stackoverflow.com/questions/40940366/ng-form-inside-ng-repeat-with-submit-button-outside-of-ng-repeat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!