【问题标题】:How to create on-change directive for AngularJS?如何为 AngularJS 创建 on-change 指令?
【发布时间】:2013-01-23 10:52:51
【问题描述】:

通常 ng-model 会在用户每次按键时更新绑定模型:

<input type="text" ng-model="entity.value" />

这几乎适用于所有情况。

但我需要在 onchange 事件发生而不是 onkeyup/onkeydown 事件时更新它。

在旧版本的 Angular 中,有一个 ng-model-instant 指令,其工作方式与现在的 ng-model 相同(至少对于用户而言——我对他们的实现一无所知)。 所以在旧版本中,如果我只是给了 ng-model,它会更新模型 onchange,而当我指定 ng-model-instant 时,它会更新模型 onkeypup。

现在我需要 ng-model 用于元素的“更改”事件。我不希望它是即时的。最简单的方法是什么?

编辑

输入仍然必须反映模型的任何其他更改 - 如果模型将在其他地方更新,则输入的值应反映此更改。

我需要的是让 ng-model 指令像在旧版本的 angularjs 中一样工作。

这是对我正在尝试做的事情的解释: http://jsfiddle.net/selbh/EPNRd/

【问题讨论】:

    标签: events angularjs


    【解决方案1】:

    在这里,我为您创建了 onChange 指令。 演示http://jsfiddle.net/sunnycpp/TZnj2/52/

    app.directive('onChange', function() {    
        return {
            restrict: 'A',
            scope:{'onChange':'=' },
            link: function(scope, elm, attrs) {
                scope.$watch('onChange', function(nVal) { elm.val(nVal); });            
                elm.bind('blur', function() {
                    var currentValue = elm.val();
                    if( scope.onChange !== currentValue ) {
                        scope.$apply(function() {
                            scope.onChange = currentValue;
                        });
                    }
                });
            }
        };        
    });
    

    【讨论】:

    • 但是我仍然希望它在模型将在其他地方更改时更新。 onchange 指令可以在 angularui (ui-event="{ change : 'changeCallback()' }) 中找到,我知道这一点。
    • 确实更新了,试试看。这就是它在演示中使用两个文本框的方式。
    • 可以在另一个指令中使用这个指令吗??
    • 在输入[type=file] 上使用它时,我收到以下错误消息:错误:操作不安全。
    【解决方案2】:

    另请参阅:AngularJS ngChange 指令。

    当应用于 时,更改发生在每次按键后,而不是模糊事件。

    http://docs.angularjs.org/api/ng.directive:ngChange

    【讨论】:

    • OP:“我需要在 onchange 事件发生而不是 onkeyup/onkeydown 事件时更新它。”
    • 另外值得注意的是,ng-change 实际上并没有绑定到 JavaScript 中的 change 事件。它实际上在ng-model 系统中设置了一个$viewValue 更改侦听器。不是一回事。
    【解决方案3】:

    Angularjs: input[text] ngChange fires while the value is changing :这个答案提供了一个更好的解决方案,允许自定义指令与 ngModel 一起使用,因此您仍然可以使用与 ngModel 一起使用的所有其他指令。

    此外,一个更灵活的解决方案,允许指定要使用的事件(不仅仅是模糊)和其他属性应该很快内置到角度:https://github.com/angular/angular.js/pull/2129

    【讨论】:

      【解决方案4】:

      我不确定是否有更好的方法来做到这一点,但您可以使用自定义指令来实现这一点(在您想要的任何 jquery 事件上)

      <input type="text" ng-model="foo" custom-event="bar" />
      <p> {{ bar }} </p>
      
      // create the custom directive
      
      app.directive('customEvent', function() {
          return function(scope, element, attrs) {
              var dest = attrs.customEvent;
      
              $(element[0]).on('any-jquery-event', function(e) {
                  e.preventDefault();
      
                  // on the event, copy the contents of model
                  // to the destination variable
                  scope[dest] = scope.foo;
      
                  if (!scope.$$phase)
                      scope.$apply();
              });
          }
      });
      

      【讨论】:

      • 我认为这不适用于最近的 angularjs。我得到一个错误 scope.apply() 函数不存在。
      猜你喜欢
      • 2016-08-16
      • 2013-12-25
      • 2019-11-21
      • 2021-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多