{"id":1796,"date":"2019-06-24T12:32:07","date_gmt":"2019-06-24T07:02:07","guid":{"rendered":"http:\/\/blog.openwebsolutions.in\/?p=1796"},"modified":"2019-06-25T12:44:47","modified_gmt":"2019-06-25T07:14:47","slug":"create-custom-pipes-angular-7","status":"publish","type":"post","link":"https:\/\/openwebsolutions.in\/blog\/create-custom-pipes-angular-7\/","title":{"rendered":"How we can create Custom Pipes in Angular 7?"},"content":{"rendered":"<h2>Custom Pipes<\/h2>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_1797\" aria-describedby=\"caption-attachment-1797\" style=\"width: 424px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" class=\" wp-image-1797\" src=\"http:\/\/blog.openwebsolutions.in\/wp-content\/uploads\/2019\/06\/image001.png\" alt=\"\" width=\"424\" height=\"247\" srcset=\"https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/06\/image001.png 520w, https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/06\/image001-300x175.png 300w\" sizes=\"(max-width: 424px) 100vw, 424px\" \/><figcaption id=\"caption-attachment-1797\" class=\"wp-caption-text\"><strong style=\"font-size: 25px; text-align: center;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span style=\"color: #3366ff;\">\u00a0<span style=\"color: #333300;\"> pipes<\/span><\/span><\/strong><\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>Pipes are a very helpful feature in Angular.\u00a0There are some\u00a0built-in pipes, but I can also build my own pipes, that is called Custom Pipe.\u00a0In this blog, we will learn about Custom Pipe in Angular7. Pipes are one of the interesting features of angular but sometimes <em>built-in pipes<\/em> are not sufficient for our formatting data, there we need to create <strong>custom pipes<\/strong>. To create a custom pipe, we need to create a<em><strong> .ts<\/strong><\/em> file and that file will be a pipe with the help of a @Pipe decorator.\u00a0Use <em>ng generate<\/em> commands to create a Custom pipe.\u00a0We are taking the date of birth from a template and from this date we are extracting the only year to get the age of the user. Instead of the Date of Birth, I want to display the current age of Employees using a custom pipe.<\/p>\n<p><strong>Let&#8217;s start&#8230;<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_1798\" aria-describedby=\"caption-attachment-1798\" style=\"width: 575px\" class=\"wp-caption alignleft\"><img loading=\"lazy\" class=\"wp-image-1798\" style=\"font-weight: bold;\" src=\"http:\/\/blog.openwebsolutions.in\/wp-content\/uploads\/2019\/06\/sc1.png\" alt=\"\" width=\"575\" height=\"226\" srcset=\"https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/06\/sc1.png 647w, https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/06\/sc1-300x118.png 300w, https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/06\/sc1-600x236.png 600w\" sizes=\"(max-width: 575px) 100vw, 575px\" \/><figcaption id=\"caption-attachment-1798\" class=\"wp-caption-text\"><span style=\"color: #3366ff;\">\u00a0 <strong>Before using custom pipe<\/strong><\/span><\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #16181a; font-family: Catamaran, helvetica, arial, sans-serif; font-size: 1.75rem; font-weight: bold;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<\/span><\/p>\n<h3><\/h3>\n<p>&nbsp;<\/p>\n<h3><\/h3>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3>app.component.html<\/h3>\n<pre>         &lt;!-- Custom Pipe--&gt; \r\n\r\n&lt;h4 [ngClass]=\"'green fs-18'\"&gt;Using Custom Pipe&lt;\/h4&gt;\r\n&lt;table [attr.border]=\"border\" [ngClass]=\"'collapse-none'\" width=100;cellpadding=\"5\" cellspacing=\"0\"&gt;\r\n    &lt;thead&gt;\r\n       &lt;tr&gt;\r\n         &lt;th [attr.colspan]=\"cols\" [ngClass]=\"['blue']\"&gt;Emp List&lt;\/th&gt;\r\n       &lt;\/tr&gt;\r\n    &lt;\/thead&gt;\r\n    &lt;thead&gt;\r\n        &lt;tr [ngClass]=\"'blue1 white'\"&gt;\r\n          &lt;th [ngClass]=\"'center'\"&gt;Id&lt;\/th&gt;\r\n          &lt;th [ngClass]=\"'center'\"&gt;Name&lt;\/th&gt;\r\n          &lt;th [ngClass]=\"'center'\"&gt;City&lt;\/th&gt;\r\n          &lt;th [ngClass]=\"'center'\"&gt;Salary&lt;\/th&gt;\r\n          &lt;th [ngClass]=\"'center'\"&gt;DOB&lt;\/th&gt; \r\n          &lt;th [ngClass]=\"'center'\"&gt;Age&lt;\/th&gt; \r\n        &lt;\/tr&gt;\r\n    &lt;\/thead&gt;\r\n    &lt;tbody&gt;\r\n       &lt;tr *ngFor=\"let member of teams ; let i=index\"&gt;\r\n          &lt;td [ngClass]=\"'center'\"&gt;10{{i+1}}&lt;\/td&gt;\r\n          &lt;td [ngClass]=\"'center'\"&gt;{{member.name}}&lt;\/td&gt;\r\n          &lt;td [ngClass]=\"'center'\"&gt;{{member.city}}&lt;\/td&gt;\r\n          &lt;td [ngClass]=\"'center'\"&gt;{{member.salary|currency:\"INR\":true}}&lt;\/td&gt;\r\n          &lt;td [ngClass]=\"'center'\"&gt;{{member.dob}}&lt;\/td&gt; \r\n          &lt;td [ngClass]=\"'center'\"&gt;<strong>{{member.dob|mypipe}}<\/strong>&lt;\/td&gt; \r\n       &lt;\/tr&gt;\r\n\r\n    &lt;\/tbody&gt;\r\n&lt;\/table&gt;\r\n<\/pre>\n<p><strong>\u00a0<\/strong>In the column of <strong><em>Age<\/em><\/strong>, I added pipe symbol ( | ) and name of a pipe(<strong>mypipe)\u00a0<\/strong>and in the column of <strong><em>Salary<\/em><\/strong>, I also used another pipe called Parameterized Pipes. After <em>currency,\u00a0<\/em>use any country code which I want to like(&#8220;<em>USD<\/em>&#8220;).<\/p>\n<p><span style=\"color: #ff0000;\">****<\/span> Here I use\u00a0 Attribute Binding (in my previous blog, I discussed Attribute Binding,\u00a0<a href=\"http:\/\/blog.openwebsolutions.in\/create-attribute-binding-angular-7\/\">http:\/\/blog.openwebsolutions.in\/create-attribute-binding-angular-7\/<\/a>),<\/p>\n<h3>app.component.scss<\/h3>\n<p>Here we modified our table using scss property.<\/p>\n<p>&nbsp;<\/p>\n<pre>.blue{\r\n\u00a0\u00a0\u00a0\u00a0color:#037FC2;\r\n}\r\n.blue1{\r\n\u00a0\u00a0\u00a0\u00a0background:#037FC2;\r\n\u00a0\u00a0\u00a0\u00a0color:#fff;\r\n}\r\n.green{\r\n\u00a0\u00a0\u00a0\u00a0color:#079E32;\r\n}\r\n.fs-18{\r\n\u00a0\u00a0\u00a0\u00a0font-size:18px;\r\n}\r\n.collapse-none{\r\n\u00a0\u00a0\u00a0\u00a0border-collapse: unset !important;\r\n\u00a0\u00a0\u00a0\u00a0width:100%;\r\n}<\/pre>\n<h3>app.component.ts<\/h3>\n<pre>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ custom pipe \/\/\r\n\r\nteams:any=[\r\n   {\r\n   'name':'John', 'city':'Delhi', 'salary':25000, 'dob':'10\/05\/1989'\r\n   },\r\n   {\r\n   'name':'Peter', 'city':'Pune', 'salary':80000, 'dob':\"12\/05\/1985\"\r\n   },\r\n   {\r\n   'name':'Om', 'city':'Goa', 'salary':150000, 'dob':\"10\/10\/1991\"\r\n   },\r\n   {\r\n   'name':'Kunal', 'city':'Delhi', 'salary':55000, 'dob':\"12\/18\/1987\"\r\n   }\r\n];<\/pre>\n<p>Now we generate <strong>mypipe.ts<\/strong>. Open the terminal and type<\/p>\n<p><strong> ng g pipe mypipe &#8211;flat <\/strong><\/p>\n<p><span style=\"color: #ff0000;\">*****<\/span><strong> &#8211;flat<\/strong> puts the file in <strong>src\/app<\/strong> instead of its own folder.<\/p>\n<p>&nbsp;<\/p>\n<h3>mypipe.ts<\/h3>\n<pre>@Pipe({\r\nname: '<strong>mypipe<\/strong>'\r\n})<\/pre>\n<div>\n<pre>export class <strong>MypipePipe<\/strong> implements PipeTransform {\r\n\r\n\u00a0 \u00a0 \u00a0transform(value: string): string {\r\n\u00a0 \u00a0 \u00a0 \u00a0let currentYear: any = new Date().getFullYear();\r\n\u00a0 \u00a0 \u00a0 \u00a0let teamsBirthYear: any = new Date(value).getFullYear();\r\n       let teamsAge = currentYear - teamsBirthYear;\r\n\r\n           return teamsAge;\r\n       }\r\n}<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_1799\" aria-describedby=\"caption-attachment-1799\" style=\"width: 594px\" class=\"wp-caption alignleft\"><img loading=\"lazy\" class=\" wp-image-1799\" src=\"http:\/\/blog.openwebsolutions.in\/wp-content\/uploads\/2019\/06\/sc2.png\" alt=\"After using Custom Pipe\" width=\"594\" height=\"235\" srcset=\"https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/06\/sc2.png 647w, https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/06\/sc2-300x119.png 300w, https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/06\/sc2-600x237.png 600w\" sizes=\"(max-width: 594px) 100vw, 594px\" \/><figcaption id=\"caption-attachment-1799\" class=\"wp-caption-text\"><span style=\"color: #3366ff;\"><strong>\u00a0 \u00a0After using Custom pipe<\/strong><\/span><\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>This is the result&#8230;<\/p>\n<p>In my next blog, I&#8217;ll discuss some other topic on Angular 7.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Custom Pipes &nbsp; &nbsp; &nbsp; Pipes are a very helpful feature in Angular.\u00a0There are some\u00a0built-in pipes, but I can also build my own pipes, that is called Custom Pipe.\u00a0In this blog, we will learn about Custom Pipe in Angular7. Pipes are one of the interesting features of angular but sometimes built-in pipes are not sufficient [&hellip;]<\/p>\n","protected":false},"author":31,"featured_media":1801,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18,5],"tags":[134],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v14.8.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How we can create Custom Pipes in Angular 7?<\/title>\n<meta name=\"robots\" content=\"index, follow\" \/>\n<meta name=\"googlebot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta name=\"bingbot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/openwebsolutions.in\/blog\/create-custom-pipes-angular-7\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How we can create Custom Pipes in Angular 7?\" \/>\n<meta property=\"og:description\" content=\"Custom Pipes &nbsp; &nbsp; &nbsp; Pipes are a very helpful feature in Angular.\u00a0There are some\u00a0built-in pipes, but I can also build my own pipes, that is called Custom Pipe.\u00a0In this blog, we will learn about Custom Pipe in Angular7. Pipes are one of the interesting features of angular but sometimes built-in pipes are not sufficient [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/openwebsolutions.in\/blog\/create-custom-pipes-angular-7\/\" \/>\n<meta property=\"og:site_name\" content=\"Openweb Solutions Blog\" \/>\n<meta property=\"article:published_time\" content=\"2019-06-24T07:02:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-06-25T07:14:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/06\/angular-custom-pipe-copy.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"350\" \/>\n\t<meta property=\"og:image:height\" content=\"255\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#website\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/\",\"name\":\"Openweb Solutions Blog\",\"description\":\"Transforming ideas into reality\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/openwebsolutions.in\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/create-custom-pipes-angular-7\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/06\/angular-custom-pipe-copy.jpg\",\"width\":350,\"height\":255},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/create-custom-pipes-angular-7\/#webpage\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/create-custom-pipes-angular-7\/\",\"name\":\"How we can create Custom Pipes in Angular 7?\",\"isPartOf\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/create-custom-pipes-angular-7\/#primaryimage\"},\"datePublished\":\"2019-06-24T07:02:07+00:00\",\"dateModified\":\"2019-06-25T07:14:47+00:00\",\"author\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#\/schema\/person\/78cc69c3015b9c840d74591261861e22\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/openwebsolutions.in\/blog\/create-custom-pipes-angular-7\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#\/schema\/person\/78cc69c3015b9c840d74591261861e22\",\"name\":\"Munmun Das\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/033db0a0941d27e1e3e3bdc05f2e1189?s=96&r=g\",\"caption\":\"Munmun Das\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/1796"}],"collection":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/comments?post=1796"}],"version-history":[{"count":16,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/1796\/revisions"}],"predecessor-version":[{"id":1816,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/1796\/revisions\/1816"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/media\/1801"}],"wp:attachment":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/media?parent=1796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/categories?post=1796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/tags?post=1796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}