Own Service in AngularJS

  1. <!DOCTYPE html>  
  2. <html>  
  3. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
  4. <body>  
  5. <div ng-app="myApp" ng-controller="myCtrl">  
  6.   
  7. <p>The hexadecimal value of 2555 is:</p>  
  8.   
  9. <h1>{{hex}}</h1>  
  10.   
  11. </div>  
  12.   
  13. <p>A custom service whith a method that converts a given number into a hexadecimal number.</p>  
  14.   
  15. <script>  
  16. var app = angular.module('myApp', []);  
  17.   
  18. app.service('hexafy', function() {  
  19.     this.myFunc = function (x) {  
  20.         return x.toString(16);  
  21.     }  
  22. });  
  23. app.controller('myCtrl', function($scope, hexafy) {  
  24.   $scope.hex = hexafy.myFunc(2555);  
  25. });  
  26. </script>  
  27.   
  28. </body>  
  29. </html>