最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin5浏览0评论

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).

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).

Share Improve this question asked Feb 10, 2016 at 9:52 membersoundmembersound 86.7k215 gold badges668 silver badges1.2k bronze badges
Add a comment  | 

3 Answers 3

Reset to default 9

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;
            })   
        }
    }
});

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

you can try using something like this.

   $scope.data=$scope.page.pageNumber+1;
   $scope.fuc=function(){
      $scope.page.pageNumber=$scope.data-1;

   };

and your Html will be like

    <input type="text"  ng-model="data" ng-change="fuc()" >

check this plunk Plunker

发布评论

评论列表(0)

  1. 暂无评论