18-service之自定义服务

来源:互联网 发布:程序员经常熬夜吗 编辑:程序博客网 时间:2024/05/21 19:37
<!DOCTYPE html><html lang="en" ng-app="myapp">    <head>        <meta charset="utf-8">        <script src="js/angular.js"></script>    </head>    <body ng-controller="myctrl">        <div>            255 的16进制是:{{hex}}<br>            255 的8进制是:{{octal}}        </div>    </body>    <script type="text/javascript">        var app = angular.module("myapp",[]);        <!-- 自定义服务hexafy,包含2个方法 myService和myService2-->        app.service('hahaha',function(){            this.myService = function(x){                return x.toString(16);            }            this.myService2 = function(x){                return x.toString(8);            }        });        <!-- 把服务传入controller中-->        app.controller("myctrl",function($scope,hahaha){            $scope.hex = hahaha.myService(255);            $scope.octal = hahaha.myService2(255);        });    </script></html>
原创粉丝点击