我正在尝试将以下JS代码转换为可重用的AngularJS过滤器
var a = 1.1230000,
b = a.toFixed(15).replace(/\.?0+$/, ""); // ==> 1.123
我创建了以下代码(AngularJS)
angular.module('myApp.filters', []).filter('fixed', [function () {
return function(input) {
return input = input.toFixed(15).replace(/\.?0+$/, "");
};
}]);
并调用新过滤器到模块
var myApp = angular.module('myApp', ['fixed']);
但显示错误: [$ injector:modulerr] http://errors.angularjs.org/1.2.16/ $ injector / modulerr?p0 = myApp& p1 =%5B%24injector%3Amodulerr%5D%20http%3A %2F%2Ferrors.angularjs.org%.........
我错过了什么,我很抱歉任何错误。谢谢你的帮助。
更新 - 已添加JSBIN URL
答案 0 :(得分:0)
您不需要注入已经是模块一部分的组件。 myApp.filter(' filterToFixed已经在模块myApp中。
答案 1 :(得分:0)
是的,您不会将filters
模块注入myApp
模块。改变你这样的代码它会对你有用
var myApp = angular.module('myApp', ['filters']);//inject your filters Module into myApp Module
angular.module('filters', []).filter('fixed', function () { //change module name as filters
return function(input) {
return input.toFixed(15).replace(/0+$/, "");
};
});
<强>溶液2 强>
根据您的代码,您可能会更改。
var myApp = angular.module('myApp', ['myApp.filters']);//inject myApp.filters into your myApp Module.
剩下的代码就像魅力......