0

I am trying to change the value of an expression based off an object of options

Currently I have an expression {{ resultItem.OdometerUnit }} which returns a total of 3 variations depending on result type

  • hours
  • kilometers
  • miles

I want to first check whats returned to expression, and then convert the value to a shorthand version based off the below values

  • hrs
  • kms
  • ms

I am thinking to do this using an object as follows:

  $scope.odometerUnitsTranslate = [
        { name: 'Kilometers', trans:'Kms' },
        { name: 'Hours', trans:'Hrs' },
        { name: 'Miles', trans:'Ms' }
  ];

Beginner with angular so not sure best approach. Any help appreciated

Thanks

1
  • 1
    You could consider writing a custom filter to do the formatting for you. Usage might look like: {{ resultItem.OdometerUnit | units }}. Commented Dec 14, 2015 at 23:29

1 Answer 1

1

JS

  $scope.odometerUnitsTranslate = 
        {'Kilometers':'Kms',
         'Hours': 'Hrs' ,
         'Miles': 'Ms' 
        };

HTML

  {{odometerUnitsTranslate[unit]}}
Sign up to request clarification or add additional context in comments.

1 Comment

To handle missing translations, I'd slightly modify it to {{odometerUnitsTransalate[unit] || unit}}. That way, if something different comes up ("feet" for example), you get "feet" instead of nothing

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.