在同一模型上发出两个指令

Bar*_*ski 6 angularjs angularjs-directive

我写了两个指令.其中一个用于输入带有单位的数字(如"1m"):

angular.module('ng', [])
.directive('unit', function() {
    return {
        require: 'ngModel',
        link: function (s, e, attributes, ngModel) {
            ngModel.$parsers.push(function (i) {
                return i + attributes.unit;
            });

            ngModel.$formatters.push(function (i) {
                return (i + '').replace(attributes.unit, '');
            });
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

第二个替换,.,因为欧洲的许多人使用小数点逗号而不是小数点,我希望我的值被标准化.

angular.module('ng', [])
.directive('numberWithPoint', function() {
    return {
        require: 'ngModel',
        link: function (s, e, attributes, ngModel) {
            ngModel.$parsers.push(function (i) {
                return i.replace(',', '.');
            });
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

用法是:

 <input type="text" ng-model="howLongSomethingIs" unit="m" number-with-point /> m
Run Code Online (Sandbox Code Playgroud)

问题:unit如果指令单独使用,该指令效果很好,如果我添加number-with-point指令,则unit不起作用(例如,显示的值1m不是1.

我试图弄乱对象priority内的return属性,但什么都没发生.

如何使这两个指令协同工作?

在这个jsfiddle,它似乎工作,但它使用Angular 1.2.1.我正在使用Angular 1.3.14.有人知道如何让jsfiddle使用其他版本吗?

Bar*_*ski 0

该错误已被发现。一件极其愚蠢的事情。我的指令在两个单独的文件中声明,每个文件都以以下内容开头:

angular.module('nameOfMyApp.directives', [])
Run Code Online (Sandbox Code Playgroud)

当然,nameOfMyApp.directives每次加载指令都会覆盖该模块。

我已将其更改为

angular.module('nameOfMyApp.directives')
Run Code Online (Sandbox Code Playgroud)

现在效果很好。