1

I created this number format directive, but if I use it on more than one input, it doesn't work for all of them, but with only one it works. Any ideas?

directive('formatUsNumber', function() {
    return {
        restrict: 'A',
        require: 'ngModel',
        priority: 100,
        link: function(scope, element, attrs, ngModel) {

            scope.formatNumber = function() {
                var n = element.val();
                var dest = n;
                if (n.length >= 10) {
                    if (/^[A-Za-z\s]+$/.test(n)) {
                        return;
                    }
                    dest = n.replace(/\D/g, '');
                    if (!isNaN(dest)) {
                        n = dest;
                    }
                    if (n.substr(0, 1) != "1") {
                        n = "1" + n;
                    }
                }
                element.val(n);
                ngModel.$setViewValue(n);
            };
        },
    };
});

The template:

<input type="text" ng-change="formatNumber()" format-us-number ng-model="myModel" />

Fiddle: http://jsfiddle.net/Lvc0u55v/7479/

3 Answers 3

1

I think it's because scope of directive is not isolated. And also I've made few changes, hope it workes the same

directive('formatUsNumber', function() {
  return {
    restrict: 'A',
    require: 'ngModel',
    priority: 100,
            scope: true,
    link: function(scope, element, attrs, ngModel) {

        scope.formatNumber = function() {
            var n = ngModel.$modelValue;
            if (n.length >= 10) {
                if (/^[A-Za-z\s]+$/.test(n)) {
                    return;
                }
                n = n.replace(/\D/g, '');
                if (!isNaN(dest)) {
                    n = dest;
                }
                if (n.substr(0, 1) != "1") {
                    n = "1" + n;
                }

                ngModel.$setViewValue(n, 'change:directive');
            }
        };
    },
  };
});

U can test it here

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, it actually did work on my fiddle, however on my app I got this error: Error: [$compile:multidir] Multiple directives [formatUsNumber, offClick] asking for new/isolated scope. Ideas?
@Brayan both directives needs isolate scope... Can u provide offClick directive body? maybe we can update it and remove usage of isolate scope?
@Brayan can u update offClick directive, as I see in their git, they've already fixed this issue
Great, will do. Thanks!
1

Try adding an isolated scope, something like this:

restrict: 'A',
require: 'ngModel',
priority: 100,
scope:{
    ngModel:'='
},...

1 Comment

Thanks, it actually did work on my fiddle, however on my app I got this error: Error: [$compile:multidir] Multiple directives [formatUsNumber, offClick] asking for new/isolated scope
1

For this use case I think that fits better implements a custom filter instead a directive. Building Custom AngularJS Filters Other alternative could be incude a custom parser and/or formatter. AngularJS - Formatters and Parsers

1 Comment

Thanks, I'll take a look.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.