Kiee
Kiee

Reputation: 10771

input show different value to whats in ng-model

Is it possible to show one value of an input but have the ng-model have a different value?

Save I have

$scope.model = { 
date: '/Date(128381238)/'
};

Then:

<input ng-model="model.date" ng-value="pretty(model.date)">

Is it possible to display the formatted date in the same input whilst unaffecting the model without having to resort to hidden inputs.

Upvotes: 0

Views: 770

Answers (1)

markau
markau

Reputation: 932

Check out filters, that's what you need; they format the data for display.

https://docs.angularjs.org/guide/filter

There are inbuilt ones like the currency example above, but you can easily write your own, here's on I wrote to convert milliseconds to a format such as "1m 36s":

.filter('base60', ['$filter', function () {
  return function (msecs) {
    var numDecimalPlaces = 1;
    var formattedElapsedTime = '';
    var seconds = (msecs / 1000).toFixed(numDecimalPlaces);
    var minutes = Math.floor(seconds/60);
    if (minutes > 0) {
      seconds = (seconds % 60).toFixed(numDecimalPlaces);
      formattedElapsedTime = minutes + 'm ' + seconds + 's';     
    } else {
      formattedElapsedTime = seconds + 's';           
    }
    return formattedElapsedTime;
 };
}])

Then on the page you just call it as such:

{{ milliseconds | base60 }}

Upvotes: 1

Related Questions