{"id":72343,"date":"2020-01-13T17:32:18","date_gmt":"2020-01-13T12:02:18","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=72343"},"modified":"2024-04-12T14:22:52","modified_gmt":"2024-04-12T08:52:52","slug":"output-formatting","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/output-formatting\/","title":{"rendered":"Output Formatting"},"content":{"rendered":"<p>AngularJS lets us format data or manipulate array collections through filters. In this example we&#8217;ll take a look at the formatting filters.<\/p>\n<p>To specify a filter in the HTML template, we can use the common data biding notation {{expression}} and add a pipe (|) after the expression to format its result, so we&#8217;ll have something like this {{expression | filter}}. If the filter accepts some parameters, we can specify them in sequence with the colon (:) character {{expression | filter:param1:param2}}.<\/p>\n<p>Angular exposes a simple API for creating a filter. Just as you would declare a controller with app.controller(\u2018myCtrl&#8217;, function(){});, you can create a new filter by appending .filter(\u2018filterName&#8217;, function(){}) to your Angular app.<\/p>\n<h4>Formatting Credit card and Telephone Number<\/h4>\n<p><strong>Requirement<\/strong> &#8211; To filter currency to format 10 digits to (555) 555-5255 and also have a credit card field that is mapped to AngularJS, like:<\/p>\n<p>&lt;input type=&#8221;text&#8221; ng-model=&#8221;customer.creditCardNumber&#8221;&gt;<\/p>\n<p>which usually returns the whole number (4111111111111111). But to format credit card data to mask it with xxx the first 12 digits and only show the last 4.<\/p>\n<p><strong>Solution<\/strong> &#8211; We make use of a custom filter as<\/p>\n<p>angular.module(&#8216;ng&#8217;).filter(&#8216;tel&#8217;, function () {<\/p>\n<p>return function (tel) {<\/p>\n<p>if (!tel) { return &#8221;; }<\/p>\n<p>var value = tel.toString().trim().replace(\/^\\+\/, &#8221;);<\/p>\n<p>if (value.match(\/[^0-9]\/)) {<\/p>\n<p>return tel;<\/p>\n<p>}<\/p>\n<p>var country, city, number;<\/p>\n<p>switch (value.length) {<\/p>\n<p>case 10: \/\/ +1PPP####### -&gt; C (PPP) ###-####<\/p>\n<p>country = 1;<\/p>\n<p>city = value.slice(0, 3);<\/p>\n<p>number = value.slice(3);<\/p>\n<p>break;<\/p>\n<p>case 11: \/\/ +CPPP####### -&gt; CCC (PP) ###-####<\/p>\n<p>country = value[0];<\/p>\n<p>city = value.slice(1, 4);<\/p>\n<p>number = value.slice(4);<\/p>\n<p>break;<\/p>\n<p>case 12: \/\/ +CCCPP####### -&gt; CCC (PP) ###-####<\/p>\n<p>country = value.slice(0, 3);<\/p>\n<p>city = value.slice(3, 5);<\/p>\n<p>number = value.slice(5);<\/p>\n<p>break;<\/p>\n<p>default:<\/p>\n<p>return tel;<\/p>\n<p>}<\/p>\n<p>if (country == 1) {<\/p>\n<p>country = &#8220;&#8221;;<\/p>\n<p>}<\/p>\n<p>number = number.slice(0, 3) + &#8216;-&#8216; + number.slice(3);<\/p>\n<p>return (country + &#8221; (&#8221; + city + &#8220;) &#8221; + number).trim();<\/p>\n<p>};<\/p>\n<p>});<\/p>\n<p>Then use this filter in template<\/p>\n<p>{{ phoneNumber | tel }}<\/p>\n<p>&lt;span ng-bind=&#8221;phoneNumber | tel&#8221;&gt;&lt;\/span&gt;<\/p>\n<h4>Preserve HTML<\/h4>\n<p>By default, AngularJS will turn your HTML into normal text, in other words: make it safe so that by default people can\u2019t insert any unwanted HTML into your page.<\/p>\n<p>For this example, let\u2019s assume you have some data that needs to be displayed on the page and that you have made an AngularJS App for it. I called mine commentApp with some spoofed (fake) data that I just hardcoded.<\/p>\n<p>We\u2019ll assume that you have replaced line breaks (if necessary) with a &lt;br \/&gt;<\/p>\n<p>var commentApp = angular.module(&#8216;commentApp&#8217;, []);<\/p>\n<p>commentApp.controller(&#8216;CommentCtrl&#8217;, function ($scope) {<\/p>\n<p>$scope.comments = [<\/p>\n<p>{<\/p>\n<p>&#8216;name&#8217; : &#8216;Some Guy&#8217;,<\/p>\n<p>&#8216;text&#8217; : &#8216;This is cool.&#8217;<\/p>\n<p>},<\/p>\n<p>{<\/p>\n<p>&#8216;name&#8217; : &#8216;John Doe&#8217;,<\/p>\n<p>&#8216;text&#8217;: &#8216;Hi, I am John Doe. This is a single-line comment to be displayed with AngularJS&#8217;<\/p>\n<p>},<\/p>\n<p>{<\/p>\n<p>&#8216;name&#8217; : &#8216;Jane Doe&#8217;,<\/p>\n<p>&#8216;text&#8217; : &#8216;Jane doe is the female version of &#8220;John Doe&#8221;.&lt;br \/&gt;This is a new line to be displayed with AngularJS&#8217;<\/p>\n<p>}<\/p>\n<p>];<\/p>\n<p>});<\/p>\n<p>You could then use the following HTML to output the data:<\/p>\n<p>&lt;section id=&#8221;comments&#8221; ng-app=&#8221;commentApp&#8221; ng-controller=&#8221;CommentCtrl&#8221;&gt;<\/p>\n<p>&lt;ul id=&#8221;comment-list&#8221;&gt;<\/p>\n<p>&lt;li ng-repeat=&#8221;comment in comments&#8221;&gt;<\/p>\n<p>&lt;a href=&#8221;#&#8221; class=&#8221;profile-link&#8221;&gt;<\/p>\n<p>{{comment.name}}<\/p>\n<p>&lt;\/a&gt;<\/p>\n<p>&lt;p&gt;<\/p>\n<p>{{comment.text}}<\/p>\n<p>&lt;\/p&gt;<\/p>\n<p>&lt;\/li&gt;<\/p>\n<p>&lt;\/ul&gt;<\/p>\n<p>&lt;\/section&gt;<\/p>\n<p>The problem now is, that instead of a new line, the text \u201c&lt;br \/&gt;\u201d will be part of the comment text.<\/p>\n<p>You now have to use $sce to run your output through a filter, which you can specify to allow the HTML.<\/p>\n<p>We can implement this using $sce.trustAsHtml() by adding a filter to our code (outside of the controller). This custom filter will make sure that our HTML doesn\u2019t get filtered out by AngularJS 1.2\/AngularJS 1.3 or later<\/p>\n<p>We will name this filter \u201cunsafe\u201d. It gets passed a value, which we will return as trusted HTML output.<\/p>\n<p>commentApp.filter(&#8216;unsafe&#8217;, function($sce) {<\/p>\n<p>return function(val) {<\/p>\n<p>return $sce.trustAsHtml(val);<\/p>\n<p>};<\/p>\n<p>});<\/p>\n<p>Now, modify your HTML code from this:<\/p>\n<p>&lt;p&gt;<\/p>\n<p>{{comment.text}}<\/p>\n<p>&lt;\/p&gt;<\/p>\n<p>And change it to this:<\/p>\n<p>&lt;p ng-bind-html=&#8221;comment.text | unsafe&#8221;&gt;&lt;\/p&gt;<\/p>\n<p>This will run your comment.text through the \u201cunsafe\u201d filter we just created, and once again, your output will have the HTML parsed properly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>AngularJS lets us format data or manipulate array collections through filters. In this example we&#8217;ll take a look at the formatting filters. To specify a filter in the HTML template, we can use the common data biding notation {{expression}} and add a pipe (|) after the expression to format its result, so we&#8217;ll have something&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[8417],"tags":[8429],"class_list":["post-72343","page","type-page","status-publish","hentry","category-angular-js-web-development","tag-output-formatting"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Output Formatting - Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/output-formatting\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Output Formatting - Tutorial\" \/>\n<meta property=\"og:description\" content=\"AngularJS lets us format data or manipulate array collections through filters. In this example we&#8217;ll take a look at the formatting filters. To specify a filter in the HTML template, we can use the common data biding notation {{expression}} and add a pipe (|) after the expression to format its result, so we&#8217;ll have something...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/output-formatting\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:52:52+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/output-formatting\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/output-formatting\/\",\"name\":\"Output Formatting - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2020-01-13T12:02:18+00:00\",\"dateModified\":\"2024-04-12T08:52:52+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/output-formatting\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/output-formatting\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/output-formatting\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Output Formatting\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Output Formatting - Tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/output-formatting\/","og_locale":"en_US","og_type":"article","og_title":"Output Formatting - Tutorial","og_description":"AngularJS lets us format data or manipulate array collections through filters. In this example we&#8217;ll take a look at the formatting filters. To specify a filter in the HTML template, we can use the common data biding notation {{expression}} and add a pipe (|) after the expression to format its result, so we&#8217;ll have something...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/output-formatting\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:52:52+00:00","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/output-formatting\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/output-formatting\/","name":"Output Formatting - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2020-01-13T12:02:18+00:00","dateModified":"2024-04-12T08:52:52+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/output-formatting\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/output-formatting\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/output-formatting\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Output Formatting"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/72343","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=72343"}],"version-history":[{"count":3,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/72343\/revisions"}],"predecessor-version":[{"id":72532,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/72343\/revisions\/72532"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=72343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=72343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=72343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}