angularjs - How can i convert text type input to number? -
i confused filter.
we can use filter attributes {{ num | number }}
why can't use this?
<input type="text" name="name" ng-model="num" /> <input type="number" name="name" ng-model="num | number" />
if approach wrong. how can convert text type input number?
you can not use filter in ng-model because ng-model 2 way binding , filter allows output if filter used ng-model can not bind new value have call filter in controller when text value changes ex. below
<input type="text" name="name" ng-model="num" /> <input type="number" name="name" ng-model="numformatted" />
consider ng-model different both inputs , numformatted manipulated in controller filter
in controller
function yourcontroller($scope,$filter) { $scope.$watch('num', function(newvalue) { $scope.numformatted = $filter('number')(newvalue); // here convert changed value of num number using filter }); $scope.num = "1"; }
so whenever num model changed using input type="text" convert number , assign numformatted type="number" updated.
Comments
Post a Comment