是否可以让AngularJS国际化数字输入?

Alb*_*reo 10 html localization cross-browser angularjs

我们正试图让AngularJS国际化<input type='number' />价值观.

我们已经包含了本地化文件(例如angular-locale_it-it.js),但仍然使用英语区域设置显示这些值.

这是一个问题,因为我们的后端(和管理)期望数值在用户的语言环境中,而接收123.45而不是123,45导致错误.

你可以在jsFiddle上找到一个例子.

它适用于Chrome 27

Chrome中的结果屏幕截图

但它在Firefox 21和Internet Explorer 10中不起作用.

Firefox中结果的屏幕截图

And*_*M96 3

这可能不是您想要的答案,但也许对您有一点帮助:

\n\n

JsBin: http: //jsbin.com/iyulaj/1/

\n\n

使用指令,您可以获取输入的数据,解析它,然后将其放入变量中$scope。请参阅: http: //docs.angularjs.org/api/ng.directive :ngModel.NgModelController ($parsers$formatters。我从:在输入元素中使用 angularjs 过滤器)。

\n\n

该指令可以搜索逗号并将其替换为点。它可能看起来像这样:

\n\n
angular.module(\'MyModule\').directive(\'numberinput\', function () {\n    return {\n        restrict: \'A\',\n        require: \'ngModel\',\n        link: function(scope, element, attrs, ngModelController) {\n\n            ngModelController.$parsers.push(function(data) {\n                //convert data from view format to model format\n                return data.replace(\',\', "."); //converted\n            });\n\n            ngModelController.$formatters.push(function(data) {\n                //convert data from model format to view format\n                return data.replace(\'.\', ","); //converted\n            });\n\n            // http://docs.angularjs.org/api/ng.directive:ngModel.NgModelController\n\n        }\n    };\n});\n
Run Code Online (Sandbox Code Playgroud)\n\n

输入字段:

\n\n
<input ng-model=\'value\' type=\'text\' numberinput=\'\' />\n
Run Code Online (Sandbox Code Playgroud)\n\n

另请注意,输入现在是一个文本字段。看看这个: http: //jsbin.com/iyurol/1/

\n\n

输入是数字字段。但是输入11,5它就会输出\xc3\xb9ndefined(请注意,我住在德国,我们使用逗号符号)。因此,如果数字不是点表示法,浏览器就无法正确“解析”该数字,即使您居住在使用逗号表示法的地区...

\n\n

如果有什么问题,请纠正我;)

\n