指令包含S /。符号

时间:2017-03-30 10:02:03

标签: javascript angularjs angularjs-directive

我想要一个指令将S/.添加到文本框的值。为此,我有一个Directive.在给定的小提琴中有一个$符号。就像我需要S/.而不是$。在那个小提琴$没有由用户提供它是内置的。如何获得S/.符号。

为此,有一个指令,但它不符合我的要求100%。这是fiddle.第一个指令根本不影响我的ng模型。这个影响了ng-model。

$ fiddle: -

app.directive('realTimeCurrency', function ($filter, $locale) {
alert("directive")
    var decimalSep = $locale.NUMBER_FORMATS.DECIMAL_SEP;
    var toNumberRegex = new RegExp('[^0-9\\' + decimalSep + ']', 'g');
    var trailingZerosRegex = new RegExp('\\' + decimalSep + '0+$');
    var filterFunc = function (value) {
        return $filter('currency')(value);
    };

    function getCaretPosition(input){
        if (!input) return 0;
        if (input.selectionStart !== undefined) {
            return input.selectionStart;
        } else if (document.selection) {
            // Curse you IE
            input.focus();
            var selection = document.selection.createRange();
            selection.moveStart('character', input.value ? -input.value.length : 0);
            return selection.text.length;
        }
        return 0;
    }

    function setCaretPosition(input, pos){
        if (!input) return 0;
        if (input.offsetWidth === 0 || input.offsetHeight === 0) {
            return; // Input's hidden
        }
        if (input.setSelectionRange) {
            input.focus();
            input.setSelectionRange(pos, pos);
        }
        else if (input.createTextRange) {
            // Curse you IE
            var range = input.createTextRange();
            range.collapse(true);
            range.moveEnd('character', pos);
            range.moveStart('character', pos);
            range.select();
        }
    }

    function toNumber(currencyStr) {
    alert("tonumber")
        return parseFloat(currencyStr.replace(toNumberRegex, ''), 10);
    }

    return {

        restrict: 'A',
        require: 'ngModel',
        link: function postLink(scope, elem, attrs, modelCtrl) {    
          alert("return");
            modelCtrl.$formatters.push(filterFunc);
            modelCtrl.$parsers.push(function (newViewValue) {
                var oldModelValue = modelCtrl.$modelValue;
                var newModelValue = toNumber(newViewValue);
                modelCtrl.$viewValue = filterFunc(newModelValue);
                var pos = getCaretPosition(elem[0]);
                elem.val(modelCtrl.$viewValue);
                var newPos = pos + modelCtrl.$viewValue.length -
                                   newViewValue.length;
                if ((oldModelValue === undefined) || isNaN(oldModelValue)) {
                    newPos -= 3;
                }
                setCaretPosition(elem[0], newPos);
                return newModelValue;
            });
        }
    };
});

S /。小提琴: -

app.directive('format', ['$filter', '$compile', function($filter, $compile) {
  alert("directive")
    return {
      require: 'ngModel',
      scope: {
        val: '=val'
      },
      link: function(scope, elem, attrs, ctrl) {

        if (!ctrl) return;
        ctrl.$formatters.unshift(function(a) {

          if (attrs.symbol == '$')
            return $filter(attrs.format)(ctrl.$modelValue, '$')
          else
            return $filter(attrs.format)(ctrl.$modelValue)
        });

                scope.$watch(function() {
            return elem.val()
        }, function(newVal, oldVal) {
          var a = newVal;
          var plainNumber = a.split('.').filter(function(e) {
            return (e.length > 0);
          }).join('.');
          var i = 0;
          if (isNaN(parseFloat(plainNumber))) {
            i = (attrs.symbol == '$') ? 1 : 3;
          }
          var num = plainNumber.substring(i, plainNumber.length).replace(/,/g, '');
          if (attrs.symbol == '$')
            elem.val($filter('currency')(num, attrs.symbol));
          else
            elem.val($filter('currency')(num));
          var n = parseFloat(num);
          scope.val = Number(n);
          if (!scope.$$phase) {
            $compile(elem.contents())(scope)
          }
        });
      }
    };

    // controller 
  }])

0 个答案:

没有答案