0

I had the following expression in Angular:

<br/><i>{{getFieldValue(teamMember.reportingData.fields, fieldname, 'meanValue' | number:2)}}</i>

where getFieldValue is a function on my controller. This was working as expected, and truncating the numeric result to 2 decimal places.

However, sometimes getFieldValue returns a string result. When that happens, it is not displayed at all. This is probably because truncating a string to 2 decimal places does not make sense.

To get round this, I tried to move the filter inside getFieldValue and only apply it to numeric results. To do this, I need a way of specifying a filter in Javascript, not as an expression in HTML. According to the docs this is possible, but the explanation is hard to follow.

Here is my attempt:

HTML:

<br/><i>{{getFieldValue(teamMember.reportingData.fields, fieldname, 'meanValue')}}</i>

part of getFieldValue :

if (field.numeric) {
   fieldValue = $filter('number')([], field[aggregate], 2);
 } else {
   fieldValue = field[aggregate];
 }   

This does not work, I get what seems to be an empty string back. How to I do this properly?

2 Answers 2

2

Use like this

 fieldValue = $filter('number')(field[aggregate], 2);

See Documentation of number filter.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for that. The doc page is much clearer than the filter doc page I had seen. Works now.
I will when it lets me :)
1
   var isnum = angular.isNumber(field[aggregate]);

    if(isnum){   
        fieldValue = $filter('number')(field[aggregate], 2) 
    }else{   
        fieldValue = field[aggregate]; 
     }

number filter in angularjs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.