未加星标

Angular 2 Minus Sign To Parentheses Pipe

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二05 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

I recently wrote up an answer to this Stack Overflow question , and I was so proud of it that I decided to write up a blog post about it.

The Problem

the basic gist of the question was that the original poster was using the built-in currency pipe but wanted to show a negative number with parentheses instead of with a minus sign.

The Solution

My solution involves chaining another custom pipe after the currency pipe. Since the currency pipe outputs a string my custom pipe can just do some string manipulation and return a new one with parentheses instead of minus signs.

<h1>{{someNumber | currency:'USD':true | myCustomPipe}}</h1> The Code

Here's the code for the custom pipe:

@Pipe({ name: 'minusSignToParens' }) export class MinusSignToParens implements PipeTransform { transform(value: any, args?: any): any { return value.charAt(0) === '-' ? '(' + value.substring(1, value.length) + ')' : value; }

The logic can be interpreted in plan english like this, "If the first character of my input value is not a minus sign then just return the value, but if it is a minus sign then return the entire string without that first character and append a string of a single paren on either side of it".

Here's an example of how you might use this in a component:

@Component({ selector: 'my-app', template: ` <h2>MinusSignToParens Pipe Demo</h2> <h1>{{someNumber | currency:'USD':true | minusSignToParens}}</h1> ` }) export class App implements OnInit { private someNumber:any = -5; constructor() {} ngOnInit() {} } The Plunk

I created a plunkr demo for this pipe that acts as a minimum workable example. I like starting from this Angular 2.4.1 plunkr starter . This starter plunk lays out all of the Angular 2 boilerplate for setting up a new project. You can create and import files right there in Plunkr so it feels (sort of) close to "real" development like you would do locally with an IDE. I am not a fan of using Plunkr for too many things, but one thing I think it is really excellent for is creating small demo projects that serve as answers to stack overflow questions (or that go along with questions as well).

The Glory

It can be nice racking up stack overflow reputation points, showing off your Angular 2 knowledge, and flexing problem solving muscles, but I also like to think that I'm helping others and making a positive impact on the world. It felt great seeing the original posters thank you comment on my answer:


Angular 2 Minus Sign To Parentheses Pipe

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

分页:12
转载请注明
本文标题:Angular 2 Minus Sign To Parentheses Pipe
本站链接:http://www.codesec.net/view/522992.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(59)