admin管理员组文章数量:1432196
There is a string expression {{zipcode}}
that displays 5 or 9 digit number.
What's the best way to display this zip-code in xxxxx
or xxxxx-xxxx
format automatically?
I believe using filter is the way to go, but slightly confused with filter and ui-mask.
Thank you.
There is a string expression {{zipcode}}
that displays 5 or 9 digit number.
What's the best way to display this zip-code in xxxxx
or xxxxx-xxxx
format automatically?
I believe using filter is the way to go, but slightly confused with filter and ui-mask.
Thank you.
Share Improve this question asked May 14, 2015 at 16:11 HaradzieniecHaradzieniec 9,34633 gold badges122 silver badges227 bronze badges1 Answer
Reset to default 5Using filters is indeed the solution for this. Here are two solutions:
Use a module
You can add angular-zipcode-filter in your project and format zip code using this filter:
{{ 981222735 | zipcode }}
Do it yourself
Here is how this filter works:
- Receives an input
- Verifies that it has 5 or 9 digits
- Returns a formatted zipcode output if it has 9 digits
- Leave it as is in case it has 5 digits
Example:
angular.module('myApp',[])
.filter('zipcode', function () {
return function (input) {
if (!input) {
return input;
}
if (input.toString().length === 9) {
return input.toString().slice(0, 5) + "-" + input.toString().slice(5);
} else if (input.toString().length === 5) {
return input.toString();
} else {
return input;
}
};
});
<script src="https://ajax.googleapis./ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<p>5-digit zip code: {{ 981222735 | zipcode }} </p>
<p>9-digit zip code: {{ 98122 | zipcode }} </p>
</div>
本文标签: javascriptAngularJS format the 5 or 9 digit zipcode with dashStack Overflow
版权声明:本文标题:javascript - AngularJS: format the 5 or 9 digit zip-code with dash - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745581987a2664675.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论