Skip nested forms validation with AngularJS

后端 未结 12 2084
失恋的感觉
失恋的感觉 2020-12-23 18:16

How can I skip validation of nested forms with AngularJS? I have to make an outer form valid even when its child form is invalid.

In the example below outer form sh

12条回答
  •  别那么骄傲
    2020-12-23 18:28

    At least with Angular 1.5 it seems to be enough to remove the nested form from the parent using $removeControl:

    module.directive('isolateForm', function() {
      return {
        restrict: 'A',
        require: '?form',
        link: function(scope, element, attrs, formController) {
          if (!formController) {
            return;
          }
    
          var parentForm = formController.$$parentForm; // Note this uses private API
          if (!parentForm) {
            return;
          }
    
          // Remove this form from parent controller
          parentForm.$removeControl(formController);
        }
      };
    });
    

    Et voila, pristine and validity states of the parent are no longer affected by the nested form.

提交回复
热议问题