{"id":688,"date":"2023-11-07T16:01:33","date_gmt":"2023-11-07T22:01:33","guid":{"rendered":"https:\/\/kop.lat\/blog\/?p=688"},"modified":"2023-11-07T16:01:33","modified_gmt":"2023-11-07T22:01:33","slug":"angular-pipe-example","status":"publish","type":"post","link":"https:\/\/kop.lat\/blog\/angular-pipe-example\/","title":{"rendered":"Angular pipe example"},"content":{"rendered":"\n<p>To obtain the <code>gini.2019<\/code> value from an object in Angular using a pipe<\/p>\n\n\n\n<p>A custom pipe can be created to access nested properties of the object. <\/p>\n\n\n\n<ol>\n<li><strong>Create a Custom Pipe:<\/strong> First, you need to create a custom pipe. You can use the Angular CLI to generate a new pipe:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>   ng generate pipe gini<\/strong><\/code><\/pre>\n\n\n\n<p>This will create a <code>gini.pipe.ts<\/code> file.<\/p>\n\n\n\n<ol start=\"2\">\n<li><strong>Implement the Custom Pipe:<\/strong> In the <code>gini.pipe.ts<\/code> file, implement the custom pipe to extract the <code>gini.2019<\/code> value from the object:<\/li>\n<\/ol>\n\n\n\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\" snippet-height=\"\" style=\"background-color:#5E69FF\"><div class=\"control-language\"><div class=\"dm-buttons\"><div class=\"dm-buttons-left\"><div class=\"dm-button-snippet red-button\"><\/div><div class=\"dm-button-snippet orange-button\"><\/div><div class=\"dm-button-snippet green-button\"><\/div><\/div><div class=\"dm-buttons-right\"><a id=\"dm-copy-raw-code\"><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span><span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a><\/div><\/div><pre class=\" line-numbers\"><code id=\"dm-code-raw\" class=\" no-wrap language-typescript\">   import { Pipe, PipeTransform } from '@angular\/core';\n\n   @Pipe({\n     name: 'gini2019'\n   })\n   export class Gini2019Pipe implements PipeTransform {\n     transform(data: any): number | undefined {\n       if (data &amp;&amp; data.gini &amp;&amp; data.gini['2019']) {\n         return data.gini['2019'];\n       }\n       return undefined;\n     }\n   }<\/code><\/pre><\/div><\/div>\n\n\n\n<ol start=\"3\">\n<li><strong>Use the Custom Pipe in a Component:<\/strong> In your Angular component&#8217;s template, you can use the custom pipe to display the <code>gini.2019<\/code> value from your object:<\/li>\n<\/ol>\n\n\n\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\" snippet-height=\"\" style=\"background-color:#7FC8A9\"><div class=\"control-language\"><div class=\"dm-buttons\"><div class=\"dm-buttons-left\"><div class=\"dm-button-snippet red-button\"><\/div><div class=\"dm-button-snippet orange-button\"><\/div><div class=\"dm-button-snippet green-button\"><\/div><\/div><div class=\"dm-buttons-right\"><a id=\"dm-copy-raw-code\"><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span><span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a><\/div><\/div><pre class=\" no-line-numbers\"><code id=\"dm-code-raw\" class=\" no-wrap language-typescript\">   &lt;div>\n     Gini (2019): {{ data | gini2019 }}\n   &lt;\/div><\/code><\/pre><\/div><\/div>\n\n\n\n<ol start=\"4\">\n<li><strong>Import the Custom Pipe:<\/strong> Make sure to import and declare the custom pipe in your module (e.g., <code>app.module.ts<\/code>):<\/li>\n<\/ol>\n\n\n\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\" snippet-height=\"\" style=\"background-color:#FB8CFF\"><div class=\"control-language\"><div class=\"dm-buttons\"><div class=\"dm-buttons-left\"><div class=\"dm-button-snippet red-button\"><\/div><div class=\"dm-button-snippet orange-button\"><\/div><div class=\"dm-button-snippet green-button\"><\/div><\/div><div class=\"dm-buttons-right\"><a id=\"dm-copy-raw-code\"><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span><span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a><\/div><\/div><pre class=\" no-line-numbers\"><code id=\"dm-code-raw\" class=\" no-wrap language-typescript\">   import { Gini2019Pipe } from '.\/gini.pipe';\n\n   @NgModule({\n     declarations: [Gini2019Pipe, \/* other components and pipes *\/],\n     \/\/ ...\n   })\n   export class AppModule { }<\/code><\/pre><\/div><\/div>\n\n\n\n<p>Replace <code>data<\/code> with the variable that holds your object. <\/p>\n\n\n\n<p>The <code>gini.2019<\/code> value will be displayed in your component&#8217;s template.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>To obtain the gini.2019 value from an object in Angular using a pipe A custom pipe can be created to access nested properties of the object. This will create a gini.pipe.ts file. Replace data with the variable that holds your object. The gini.2019 value will be displayed in your component&#8217;s template.<\/p>\n","protected":false},"author":1,"featured_media":259,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[109,49],"tags":[102,103,105,63],"_links":{"self":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/688"}],"collection":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/comments?post=688"}],"version-history":[{"count":1,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/688\/revisions"}],"predecessor-version":[{"id":689,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/688\/revisions\/689"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media\/259"}],"wp:attachment":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media?parent=688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/categories?post=688"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/tags?post=688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}