AngularJS: Prevent hidden form fields being validated

前端 未结 3 1196
Happy的楠姐
Happy的楠姐 2021-01-31 02:08

What is the best way of preventing hidden form fields being validated in AngularJS?

3条回答
  •  失恋的感觉
    2021-01-31 02:28

    You can remove required attribute by using directives:

    var app = angular.module('myApp',[]);
    
    app.directive('input',function($compile){
      return {
        restrict:'E',
        compile:function($tElement,$tAttrs){
            console.log("hi there");
          var el = $tElement[0];
          if(el.getAttribute('type')){
            el.removeAttribute('type');
            el.setAttribute($tAttrs.type,'');
            return function(scope){
              $compile(el)(scope);
            }
          }
    
        }  
      }
    });
    
    
    app.directive('remove',function($compile){
      return {
        restrict: 'A',
        replace:true,
        template:'',
          link: function (scope, element, attrs) {
              element.removeAttr('required');
          }
      }
    });
    

    See Fidlle here

    Before:

    
    

    After:

    
    

提交回复
热议问题