Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show different value of input element with ng-model?

In the controller if have a variable that tracks the index (starting at 0) of the page for a pagination table:

var page {
  pageNumber: 0;
}

Question: how can I show this pageNumber variable in the html, but always incremented by +1? (as the index=0 page is obviously the 1st page and should thus be shown as Page 1)

<input type="text" ng-model="page.pageNumber">

Also, when the model gets updated, the value in the input should automatically change (again: also incremented by +1).

like image 884
membersound Avatar asked Feb 10 '16 09:02

membersound


People also ask

How do you change the value of an NG-model?

If we use two way binding syntax for ngModel the value will be updated. So the default (ngModelChange) function will update the value of ngModel property. i.e., user.Name . And the second (ngModelChange) will be triggered printing the user name value in the console.

Can we use filter in NG-model?

By setting the ng-model directive on an input field, we can use the value of the input field as an expression in a filter.

What is NG-model options Debounce?

The updateOn and debounce properties allow you to specify a custom list of events that will trigger a model update and/or a debouncing delay so that the actual update only takes place when a timer expires; this timer will be reset after another change takes place.


2 Answers

I think this is a use-case for $formatters and $parsers. They operate on the model's property and there is no need to create a dummy property on the model. Documentation here. Please correct me if this is not the use case for $formatters and $parsers.

Please see below.

HTML markup

<body ng-app="app" ng-controller="mainCtrl">   
   {{page}}
   <input paginated-index type="text" ng-model="page">   
</body>

js

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

app.controller('mainCtrl', function($scope) {
  $scope.page = 0;
});

app.directive('paginatedIndex', 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;
            })   
        }
    }
});
like image 122
Naga Sandeep Avatar answered Oct 12 '22 11:10

Naga Sandeep


In your controller, change your page object to this:

$scope.page = {
  displayedPage: function(num) {
    if(arguments.length) {
       $scope.page.pageNumber = num - 1;
       return num;
    } else {
      return $scope.page.pageNumber + 1;
    }
  },
  pageNumber: 0
}

And then yourelement to this:

<input type="text" ng-model="page.displayedPage" ng-model-options="{ getterSetter: true}" />

This will display the page number plus 1, but leave the actual page.pageNumber variable the way it should be.

The getterSetter: true options I've added in will bind the model to a getter/setter function, which allows you to pass in the argument - in this case, your entered page number - and return from that function. You can read more information on this in the documentation for ngModel

like image 33
millerbr Avatar answered Oct 12 '22 10:10

millerbr