2

I have this structure in a controller in angularjs:

$scope.modelInfo = {"key1":value1, "key2": value2,.....};

In the view:

<tr ng-repeat="(key,value) in modelInfo">
    <td width="60%">{{key}}</td>
    <td>{{value}}</td>
</tr>

I need if typeof value is number, print value with angular filter, something like:

{{value | number : 4}}
0

3 Answers 3

2

Its really annoying that you haven't had datatypes to your properties. Its its not good design that you had. I'd first prefer to change design such that perticular object pattern so that you could easily decide which object is holding which datatype value.

Though You could do it by creating a custom filter, by checking typeof value.

HTML

<tr ng-repeat="(key,value) in modelInfo">
    <td width="60%">{{key}}</td>
    <td>{{value | myNumber: 4}}</td>
</tr>

Code

app.filter('myNumber', function($filter){
  return function(input, precision){
     if(!input) return input;
     if(typeof input === "number")
       return $filter('number')(input, precision);
  }
})
Sign up to request clarification or add additional context in comments.

Comments

2

You can write a custom filter to achieve this:

myApp.filter('myNumber', function($filter) {
    return function(value, fractionSize) {
        if (!angular.isNumber(value)) {
            return value;
        }

        return $filter('number')(value, fractionSize);
    }
});

And then use it:

{{value | myNumber : 4}}

See a working example below:

var app = angular.module("sa", []);

app.controller("FooController", function($scope) {
$scope.modelInfo = {"key1":'value1', "key2": 2.453434};
});

app.filter('myNumber', function($filter) {
  return function(value, fractionSize) {
    if (!angular.isNumber(value)) {
      return value;
    }

    return $filter('number')(value, fractionSize);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="sa" ng-controller="FooController">
  <table>
    <tr ng-repeat="(key,value) in modelInfo">
      <td width="60%">{{key}}</td>
      <td>{{value | myNumber : 4}}</td>
    </tr>
  </table>
</div>

5 Comments

hehe, you beat me by 22 seconds :) +1
No worries Pankaj. Even then you are great :)
No one is great here, still I'm at learning stage. there is lot to learn in this world :p Thanks & cheers :)
Thanks to both. I like the two answer but only I can check one (the first).
@maikelm np, we just care you should get right answer which should be workable.. But still I'm feeling that you have something wrong on server side design as I mentioned in my answer... Thanks for good words, Cheers :)
1

one way to do this is to create an intermediate filter to test the type of your value :

app.filter('filterIfNumber', function ($filter) {
  return function (item) {
    if (angular.isNumber(item)) {
       return $filter('number')(item);
    }   
    return item;
  };
}); 

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.