JavaScript Display one value for user, but keep another in the model

I have a form where the user enters some numbers. These numbers are indexes. The problem is that when I'm posting the model to the server I need the index to be zero-based. But the index should be presented for the user as one-based.

So when the model has index 4, it should be presented to the user as 5.

Is it possible to do this? I rather not manually change it before posting, because it's actually several values, and not just one.

var myApp = angular.module('myApp',[]);


function MyCtrl($scope) {
    $scope.model = {
      index: 3
    }
    
    // Later I post the model with ajax.
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="MyCtrl">
    Enter index: <input type="number" ng-model="model.index"><br/><br/>
       
    Actual index is <strong>{{model.index}}</strong> (Should be one less than what is shown in input)
  </div>
</div>
Answer:1

You could make a directive to alter the value displayed to the user like this:

var myApp = angular.module('myApp',[]);


function MyCtrl($scope) {
    $scope.model = {
      index: 3
    }
    
    // Later I post the model with ajax.
}

myApp.directive('correctIndex', function()
{
    return{
        restrict: 'A',
        require: 'ngModel',
        link: function(scope, element, attrs, ngModelController)
        {
            ngModelController.$formatters.push(function(value)
            {
                return value+1;
            })

            ngModelController.$parsers.push(function(value)
            {
                return value-1;
            })   
        }
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="MyCtrl">
    Enter index: <input correct-index type="number" ng-model="model.index"><br/><br/>
       
    Actual index is <strong>{{model.index}}</strong> (Should be one less than what is shown in input)
  </div>
</div>
Answer:2

I have a really strange problem: I am using jQuery v11 on the latest Chrome on localhost. While I manage to use jQuery.animate() on my website with any elements and features (including opacity), I ...

I have a really strange problem: I am using jQuery v11 on the latest Chrome on localhost. While I manage to use jQuery.animate() on my website with any elements and features (including opacity), I ...

  1. jquery opacity animation example
  2. jquery opacity animation
  3. jquery animation opacity 0 to 1
  4. jquery alpha animation
  5. jquery animate opacity not working
  6. jquery animate opacity toggle
  7. jquery animate opacity 0
  8. jquery animate opacity to 1
  9. jquery animate opacity display none
  10. jquery animate opacity left to right
  11. jquery animate opacity loop
  12. jquery animate opacity on scroll
  13. jquery animate opacity then hide
  14. jquery animate opacity easing
  15. jquery animate opacity slow
  16. jquery animate opacity w3schools
  17. jquery animate opacity time
  18. jquery animate opacity doesn't work
  19. jquery animate opacity display block
  20. jquery animate opacity 0 to 100

Is there an empty Subscription in RxJs? In RxJava, there is Subscriptions.empty(). An empty Subscription is a Subscription to which unsubscribe does nothing except to change isUnsubscribed to true. ...

Is there an empty Subscription in RxJs? In RxJava, there is Subscriptions.empty(). An empty Subscription is a Subscription to which unsubscribe does nothing except to change isUnsubscribed to true. ...

  1. empty subscribe rxjs

I recently download this Slideshow with jmpress.js for a website. I'm using PHP and a database in MYSQL. I used a recordset in Dreamweaver to get the data from database to the site. Then I tried to ...

I recently download this Slideshow with jmpress.js for a website. I'm using PHP and a database in MYSQL. I used a recordset in Dreamweaver to get the data from database to the site. Then I tried to ...

I'm struggling with Facebook login via Angular.js in my Rails App. <li><a ng-click="fb_login()" href="">Login with Facebook</a></li> And on my UserCtrl $scope.fb_login = ...

I'm struggling with Facebook login via Angular.js in my Rails App. <li><a ng-click="fb_login()" href="">Login with Facebook</a></li> And on my UserCtrl $scope.fb_login = ...