如何将自定义输入指令及其父表单重置为$ pristine

时间:2015-01-30 18:56:27

标签: angularjs angularjs-directive angularjs-ng-model angularjs-ng-form

我已经实现了一个具有重置功能的自定义输入指令 - counter。该指令有require: "ngModel"

我正在使用ngModel重置指令$setPristine()的原始状态。与$setDirty()不同,$setPristine()不会触及父表单的$pristine状态。

问:如何“通知”父表单此指令不再“脏”,以便父表单可以重置其$pristine状态?

请记住,仅仅调用form.$setPristine()是不够的,因为表单中可能还有其他“脏”控件,我的指令不会(也不应该)知道。

这是指令的链接功能:

link: function(scope, element, attrs, ngModel){

  var original;

  ngModel.$render = function(){
    original = scope.counter = ngModel.$viewValue;
  };

  scope.up = function(){
    ngModel.$setViewValue(++scope.counter);
  };

  scope.reset = function(){
    scope.counter = original;
    ngModel.$setViewValue(scope.counter);
    ngModel.$setPristine(); // this sets $pristine on the directive, but not the form
  };
}

以下是它的使用方法:

<div ng-form="form">
  <counter ng-model="count"></counter>
</div>

plunker

2 个答案:

答案 0 :(得分:3)

从Angular 1.3.x开始,没有内置的解决方案。

form.$setPristine()在其所有子控件上设置原始状态。 (link to code

ngModel.$setPristine()仅设置$ pristine(link to code

解决此问题的一种方法是创建一个与form指令同时存在的指令,并劫持form.$setDirty来跟踪脏控件计数。这可能最好在预链接阶段完成(即在子控件开始注册之前)。

app.directive("pristinableForm", function() {
  return {
    restrict: "A",
    require: ["pristinableForm", "form"],
    link: function(scope, element, attrs, ctrls) {
      var me = ctrls[0],
        form = ctrls[1];
      me.form = form;
      me.dirtyCounter = 0;
      var formSetDirtyFn = form.$setDirty;
      form.$setDirty = function() {
        me.dirtyCounter++;
        formSetDirtyFn();
      };
    },
    controller: function() {
      this.$notifyPristine = function() {
        if (this.dirtyCounter === 0) return;
        if (this.dirtyCounter === 1) {
          this.dirtyCounter = 0;
          if (this.form) this.form.$setPristine();
        } else {
          this.dirtyCounter--;
        }
      };
    }
  };
});

然后,自定义输入指令需要require: ["ngModel", "^pristinableForm"]并在其重置功能中调用pristinableForm.$notifyPristine()

scope.reset = function(){
  if (ngModel.$dirty){
    scope.counter = original;
    ngModel.$setViewValue(scope.counter);
    ngModel.$setPristine();
    pristinableForm.$notifyPristine();
  }
};

用法是:

<div ng-form="form" pristinable-form>
  <counter ng-model="count1"></counter>
  <counter ng-model="count2"></counter>
  <input ng-model="foo" name="anotherControl">
</div>

plunker

答案 1 :(得分:0)

这是一个不太好的解决方案。通过附加到表单的控件进行迭代,并检查是否还有一个脏的。

我使用方法说明here来获取控件。

Plunker

app.directive("counter", function(){
  return {
    require: "ngModel",
    template: '<button ng-click="up()">{{counter}}</button><button ng-click="reset()">reset</button>',
    link: function(scope, element, attrs, ngModel){

      var original;

      ngModel.$render = function(){
        original = scope.counter = ngModel.$modelValue;
      };

      scope.up = function(){
        ngModel.$setViewValue(++scope.counter);
      };

      scope.reset = function(){
        scope.counter = original;
        ngModel.$setViewValue(scope.counter);
        ngModel.$setPristine();

        // check if one of the controls attached to the form is still dirty
        var dirty = false;
        angular.forEach(scope.form, function(val, key) {
            if (key[0] != '$') {
              if (val.$dirty) {
                dirty = true; 
              }
            }
        });
        if(!dirty) scope.form.$setPristine();


      };
    }
  };
});   
相关问题