Преобразовать директиву AngularJs в Angular

Я нахожусь в процессе преобразования AngularJS в Angular 6. Как преобразовать эту директиву для работы с Angular 6?

app.directive('numberOnly', function() {
return {
    priority: 1, // if we're unshifting, lower number priority runs later; if we're pushing, lower number priority runs earlier
    require: 'ngModel', // element must have ng-model attribute.
    restrict: 'A', // restrict to an attribute type.
    // scope = the parent scope
    // elem = the element the directive is on
    // attr = a dictionary of attributes on the element
    // ctrl = the controller for ngModel.
    link: {
        pre: function (scope, elemn, attr, ctrl) {
            var NUMBER_REGEXP = /^\s*(\-|\+)?(\d+|(\d*(\.\d*)))\s*$/;
            var testNumber = function (value) {
                var empty = ctrl.$isEmpty(value); // nifty built-in method to do null/undefined/'' checks

                if (empty) {
                    return true; // returning true avoids a false positive on $error since $setValidity inverts it.
                }
                return NUMBER_REGEXP.test(value);
            }
            ctrl.$parsers.unshift(function (value) {
                // test the validity after update.
                var valid = testNumber(value);
                // set the validity after update.
                ctrl.$setValidity('numberOnly', valid);

                // if it's valid, return the value to the model, otherwise return undefined.
                return valid ? value : undefined;
            });
            ctrl.$formatters.unshift(function(value) {
                // test the validity after update.
                var valid = testNumber(value);
                // set the validity after update.
                ctrl.$setValidity('numberOnly', valid);
                return value;
            });
        }
    }
};
});

Можно ли добавить больше функций в одну директиву?


person CoolKane    schedule 04.06.2018    source источник
comment
Что вы пробовали?   -  person Jeroen Heier    schedule 04.06.2018
comment
Меня смущает функция (scope, elemn, attr, ctrl), не знаю, что она делает. Также я новичок в кодировании   -  person CoolKane    schedule 04.06.2018
comment