AngularJS 服务(Service)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>AngularJS 服务(Service)</title>

    <script src="../js/jquery-2.1.3.min.js"></script>
    <script src="../js/angular.min.js"></script>
</head>
<body>

    <!--
        $location: window.location, 方法有 $location.absUrl()
        $http: jquery.ajax 方法有 $http.get
        $timeout: window.setTimeout 也是有区别的
        $interval: window.setInterval 也是有区别的
    -->

    <!--<div ng-app="myApp" ng-controller="myCtrl">
        创建自定义服务:
        {{hex}}
    </div>
    <script>
        var app = angular.module('myApp', []);

        app.service('hexafy', function() {
            this.myFunc = function (x) {
                return x.toString(16);
            }
        });
        app.controller('myCtrl', function($scope, hexafy) {
            $scope.hex = hexafy.myFunc(255);
        });
    </script>-->

    <!--过滤器中,使用自定义服务-->
    <div ng-app="myApp" ng-controller="myCtrl">
        <p>在获取数组 [255, 251, 200] 值时使用过滤器:</p>

        <ul>
            <li ng-repeat="x in counts">{{x | myFormat}}</li>
        </ul>

        <p>过滤器使用服务将10进制转换为16进制。</p>
    </div>

    <script>
        var app = angular.module('myApp', []);
        app.service('hexafy', function() {
            this.myFunc = function (x) {
                return x.toString(16);
            }
        });
        app.filter('myFormat',['hexafy', function(hexafy) {
            return function(x) {
                return hexafy.myFunc(x);
            };
        }]);
        app.controller('myCtrl', function($scope) {
            $scope.counts = [255, 251, 200];
        });
    </script>

</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值