Solved

Custom filter using  AngularJS

Posted on 2014-02-26
3
804 Views
Last Modified: 2014-02-26
Hi Experts,

I get a numeric value from a server and would like to format as a percent and display in the UI. How can I do this in Angularjs using custom filter? Here is my requirement

Data from server                     UI  

0                                 should display as NA
0.1234                        should display as 12.34%  //2 decimals
0.05                            should display as  5.00%  //2 decimals
6.24                            should display as  6.24%  

Thanks in Advance
0
Comment
Question by:ksd123
3 Comments
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 150 total points
ID: 39890741
Test page : http://jsfiddle.net/ausS4/

var myApp = angular .module("myApp",[]) .controller(myCtrl) .factory("Data", function() { return { get: function() { return [0,0.1234,0.05,6.24]; } } }); var myCtrl = function($scope, Data) { $scope.numbers = []; var data = Data.get(); for(var i=0;i<data.length;i++) { $scope.numbers.push( myFormatter(data[i]) ); } } var myFormatter = function(value) { value = value * 1; if(value == 0) { return "NaN"; } else if(Math.abs(value) > 1) { return value.toFixed(2) + "%"; } return (value * 100).toFixed(2) + "%"; }
0
 
LVL 25

Accepted Solution

by:
Kyle Hamilton earned 150 total points
ID: 39890874
this would be the angular filter way to do it:

angular.module('MyApp').
  filter('toPercent', function() {
    return function (value) {
		if (value == 0) {
		    value = "NA";
		} else if (Math.abs(value) >= 1){
		    value =  (value).toFixed(2) + "%";
                }else{
		    value =  (value *  100).toFixed(2) + "%";
                }
                return value;
    }
  })

Open in new window


then in the template, you can use your filter like this;

{{value|toPercent}}

Open in new window

0
 

Author Closing Comment

by:ksd123
ID: 39891143
Thank you guys
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how can i test my code? 1 38
Jquery keyup 4 19
"Pause" execution of javascript function for x miliseconds 2 26
Html form and modal / img src -problem 3 11
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question