我写了两条指令。其中一个用于输入单位数(如"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, '');
});
}
}
});
第二种方法将,
替换为.
,因为在欧洲许多人使用十进制逗号而不是小数点,我希望我的值能够规范化。
angular.module('ng', [])
.directive('numberWithPoint', function() {
return {
require: 'ngModel',
link: function (s, e, attributes, ngModel) {
ngModel.$parsers.push(function (i) {
return i.replace(',', '.');
});
}
}
});
其用途如下:
<input type="text" ng-model="howLongSomethingIs" unit="m" number-with-point /> m
问题:如果指令unit
是单独的,那么它的工作效果很好,如果我添加了number-with-point
指令,unit
就没有效果(例如,显示的值是1m
而不是1
)。
我试图在priority
对象中处理return
属性,但什么也没有发生。
如何使这两个指令协同工作?
在这个小提琴中,它似乎可以工作,但是它使用的是角1.2.1。我用的是角1.3.14。有人知道如何使用其他版本吗?
发布于 2015-07-11 20:18:35
窃听器已经被发现了。一件不可思议的蠢事。我的指令是在两个单独的文件中声明的,每个文件的开头都是:
angular.module('nameOfMyApp.directives', [])
当然,每次加载指令都会覆盖nameOfMyApp.directives
模块。
我把它改成了
angular.module('nameOfMyApp.directives')
现在它运转得很好。
发布于 2015-07-11 20:09:22
在这个弹琴中,它看起来很有效,但是它使用的是角1.2.1。我用的是角1.3.14。有人知道如何使用其他版本吗?
我只是在角1.3.15上本地运行所提供的代码-工作得很好。
当绑定到视图时,输入123071823,1238
计算结果为123071823.1238m
。因此,它似乎工作得很好(在我这边,在你的小提琴上)。
1.3.14 jsFiddle
在jsFiddle上加载不同版本:
External resource
在jsFiddle中。https://stackoverflow.com/questions/31361192
复制相似问题