18-service之自定義服務

<!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>
發佈了63 篇原創文章 · 獲贊 4 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章