简体   繁体   English

Angular 1.4.7指令绑定到所有数字输入

[英]Angular 1.4.7 directive bound to all number inputs

I'm trying to figure out if there is a way to bind a directive to all <input type="number"> elements without having to go back and add a class, additional attribute, etc. to each number field? 我试图找出是否有一种方法可以将指令绑定到所有<input type="number">元素,而不必回头向每个数字字段添加类,附加属性等?

I have a lot of number fields so I was hoping to avoid having to update them all. 我有很多数字字段,所以我希望避免必须全部更新它们。

Since it doesn't appear that there is a way to bind specifically to an input type, I just ended up creating a simple directive bound to every input and then isolated the functionality of the directive to inputs of the number type. 由于似乎没有办法专门绑定到输入类型,因此我最终创建了一个绑定到每个输入的简单指令,然后将该指令的功能隔离到数字类型的输入上。

.directive('input', ['$document', '$log', '$filter', function ($document, $log, $filter) {
    return {
      restrict: 'E',
      require:  '?ngModel',
      scope:    {
        directiveConfig:        '=',
        controllerInterface:    '='
      },

      link: function (scope, element, attrs, ngModelCtrl) {
        // --------------------------------------------------------------
        // Formatters & Parsers for `input[type="number"]` only
        // --------------------------------------------------------------
          if (attrs.type === "number") {
            // Prevents number inputs from using scrolling to change number.
            element.bind('mousewheel', function (event) {
              event.preventDefault();
            });

            // Prevents number input from changing with up/down arrow keys and
            // instead simulates tabbing. 
            element.bind("keydown keypress", function (event) {
              var inputs = element.closest('form').find(':input'); 

              switch (event.which) {
                case 38:
                  // Arrow Up: 38
                  inputs.eq( inputs.index(element) - 1 ).focus();
                  event.preventDefault();
                  break;
                case 40:
                  // Arrow Down: 40
                  inputs.eq( inputs.index(element) + 1 ).focus();
                  event.preventDefault();
                  break;
                default:
                  break;
              }
            });

            // Ensures that only numbers can be entered.
            var numericUserInput = function(value) {
              var parsed = null;

              if ($filter('exists')(value)) {
                parsed = parseFloat(value.toString().replace(/[^0-9.]+$/, ''), 10);

                if (typeof(value) != "number") {
                  parsed = null;
                }

                if (parsed !== value) {
                  ngModelCtrl.$setViewValue(parsed);
                  ngModelCtrl.$render();
                }
              }

              return parsed;
            } 

            ngModelCtrl.$parsers.push(numericUserInput);
          }

      }
    }
}])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM