Попробуйте ввести указанный ниже сервисный код:
app.service('hexafy', function() {
this.myFunc = function (x) {
return Number(x).toString(16);
}
});
app.controller('myCtrl', function($scope, hexafy) {
$scope.hex = hexafy.myFunc($scope.m5);
});
Надеюсь, это подойдет вам.
Но я предпочитаю использовать параметр фильтра для этого. Вы можете попробовать фильтр как:
AngulrJS
var app = angular.module('myApp', []);
//Filter to conver input number into hexadecimal number
app.filter('hexafy', function () {
return function(x) {
return Number(x).toString(16);
};
});
app.controller('myCtrl', ['$scope', 'hexafyFilter', function($scope, hexafyFilter) {
$scope.m5 = 0;
}]);
HTML
<div ng-app="myApp" ng-controller="myCtrl">
<p>A custom service with a method that converts a given number into a hexadecimal number.</p>
<label>input a number</label>
<input type="number" ng-model="m5"></input>
<p>The hexadecimal value of {{m5}} is:</p>
<h1>{{ m5 | hexafy }}</h1>
</div>
Наслаждайтесь решением. :)