我认为我缺少$validators$setValidity的某些内容(据我了解,它们做的是完全相同的事情,所以您不需要两者都使用-如果我错了,请纠正我)。无论我是否有$validators语句,我都会将ng-invalid类添加到输入表单,该类在输入周围添加了红色边框。那为什么我需要$validators?如果用户未从指令模板的dropdownn中选择一行,我试图将父窗体设置为无效。我不想显示任何错误消息或任何东西,我只是想根据是否在下拉列表中选择了行来添加无效的类和红色边框。

我应该使用$validators还是$setValidity?是否需要像下面一样的$validator$setValidity?另外,$setValidity是否需要ngModelCtrl?如果setValidity不在$validators函数中,则无法定义。任何帮助表示赞赏。

如果我想使父窗体在整体上无效(如果没有选择的话)并且我触摸时得到ng-invalid类,然后如果没有$validators$setValidity的情况下没有选择,则模糊处理,那为什么我需要$validators$setValidity ??

index.html

   <form name="myForm">
      <validator
         rows="[{name:'tom', city:'san fran', state: 'mn', zip: 34212},
               {name: 'joe', city:'san fran', state: 'mn', zip: 45675}]"
         ng-required="true"
         ng-model="hey">
      </validator>
  </form>


validate.js-DDO

   return {
      restrict: 'E',
      require: {
      ngModelCtrl: 'ngModel',
      formCtrl: '?^form'
   },
      replace: true,
      templateUrl: 'view.html',
      scope: {},
      controllerAs: 'ctrl',
      bindToController: {
         rows: '=',
         onSelected: '&?', //passsed selected row outside component
         typedText: '&?', //text typed into input passed outside so
                          //developer can create a custom filter,
                          //overriding the auto
         textFiltered: '@?', //text return from the custom filter
         ngRequired: "=?" //default false, when set to true the component
             //needs to validate that something was selected on blur.
             //The selection is not put into the input element all the
             //time so it can't validate based on whether or not
             //something is in the input element itself.
             //I need to validate inside the controller where I can see
             //if 'this.ngModel' (selectedRow - not passed through scope)
             //is undefined or not.
        },
         controller: 'validatorController'
  };




   function validatorController () {
         var ctrl = this;
         var rowWasSelected = false;
         var input = ctrl.formCtrl.inputField;

         //called via ng-click on the dropdown row
         //if this is called a row was selected
         ctrl.rowSelected = function (row){
             rowWasSelected = true;
         }

         //called via ng-blur of the input element
         ctrl.ngModelCtrl.$validators.invalidInput = function (modelValue, viewValue) {
               return rowWasSelected;
           }


         ctrl.$onInit = $onInit; //angular will execute this after
             //all conrollers have been initialized, only safe to use
             //bound values (through bindToController) in
             //the $onInit function.

             //i understand this need to be there with Angular 1.5
             //using ngModel in the controller
             //but I really only need to validate on ng-blur of the input
        function $onInit() {
              ctrl.validateInput();
            }
           }
         };
      }


view.html-指令模板

   <div class="dropdown" ng-class="{'open' : ctrl.isOpen}">
      <div class="form-group">
        <input type="text" class="form-control" name="inputField"
               placeholder="select" ng-click="ctrl.openDropdown()"
               ng-blur="ctrl.validateInput()"
               ng-model="ctrl.currentRow.name"
               ng-required="ctrl.ngRequired">
       </div>
   <ul class="dropdown-menu list-group">
      <li class="list-group-item" ng-repeat="row in ctrl.rows"
          ng-click="ctrl.onSelectedLocal(row)">
             {{row.name}}
      </li>
   </ul>
  </div>


无论使用什么$validators函数,我都必须获取无效的类,因为无论是否存在ng-invalid类,它都会添加类?

最佳答案

$setValidity(validationErrorKey, isValid);

更改有效性状态,并通知表单。

可以在$parsers/$formatters或自定义验证实现中调用此方法。但是,在大多数情况下,使用ngModel.$validatorsngModel.$asyncValidators集合将足以自动调用$setValidity1

关于angularjs - $ validators和$ setValidity有什么意义?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35676163/

10-11 05:38