user3038625 user3038625 - 3 months ago 19
AngularJS Question

convert returned value from ng-model

i wrote this angularjs code but the problem is when i make the input type to number every thing is ok and the number converted to hexa even i make change the number manually without increase or decrease buttons , but when i make input type to text the result is just same number of text input without converted it.
why ?? and sorry for my english !!

this is my code :

<html>
<head>
<title> convert to hex </title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8 /angular.min.js"></script>

</head>

<body>
<div ng-app="myapp" ng-controller="namectrl">
<input ng-model = 'number' type="text">
<h3> {{hexa(number)}} </h3>
</div>

<script>
var app = angular.module("myapp" ,[]);
app.controller("namectrl" , function($scope)
{
$scope.hexa = function(x) {
return x.toString(16);
};
});

</script>
</body>
</html>

Answer

Try this,

 $scope.hexa = function(x) {
          return Number(x).toString(16);
 };

Working App

Comments