Skip nested forms validation with AngularJS

后端 未结 12 2043
失恋的感觉
失恋的感觉 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:18

    Also inspired by mbernath, I found a simpler solution. It consists of creating a dummy form-like directive only for isolation. The directive stops propagation from nested elements to outer form but it doesn't have any form functionality. You can nest ngForms inside and have them fully functional.

    angular.directive('formIsolator', function () {
    
                return {
                    name: 'form',
                    restrict: 'EAC',
                    controller: function() {
                        this.$addControl = angular.noop;
                        this.$$renameControl = function(control, name) {
                            control.$name = name;
                        };
                        this.$removeControl = angular.noop;
                        this.$setValidity = angular.noop;
                        this.$setDirty = angular.noop;
                        this.$setPristine = angular.noop;
                        this.$setSubmitted = angular.noop;
                    }
                };
            })
    

    The way is to specify the name of controller in directive definition (name: 'form'). This property isn't documented, but is used for creating ngForm directive in angular source.

提交回复
热议问题