Make negative number positive, but maintain the actual value

I'm going on a whim here, but I figured I'd ask for a check here.

I have an integer value that I use to determine how other elements on the page act, the actual value of the integer is important (whether negative or positive), however it actually displays a prefix hyphen when negative. desirable.

Is there a way to "hide" the hyphen without changing the actual value of the integer? Bearing in mind that the value is $watch

ed, so multiplying it by -1 is out of the question.

Template:

<p>{{ value }}</p>

      

Directive

scope.$watch('value', function() 
{
 if (scope.value > 0)
 {
   //do something
 }
 else
 {
  //do something else
 }

});

      

Added code, not what I believe is really needed

+3


source to share


2 answers


you can use filter

.filter('positive', function() {
        return function(input) {
            if (!input) {
                return 0;
            }

            return Math.abs(input);
        };
    })

      



so in your template use

{{value | positive}}

      

+10


source


Pass it through a function like this:

View

<p>{{ positiveLookingValue() }}</p>

      



controller

$scope.positiveLookingValue = function(){
  return Math.abs($scope.value);
}

      

+1


source







All Articles