AngularJs通过service自定义服务

与factory类似,AngularJs也可以通过service来自定义服务,只是在return返回数据的写法上稍有不同,具体代码如下:

HTML部分:

01<body ng-app="app" ng-controller="ctr" ng-cloak class="ng-cloak">
02    <table cellpadding="0" cellspacing="0">
03        <thead>
04            <tr>
05                <td>网站名称</td>
06                <td>网站地址</td>
07            </tr>
08        </thead>
09        <tbody>
10            <tr ng-repeat="(key, value) in data">
11                <td>{{value.webname}}</td>
12                <td>{{value.weburl}}</td>
13            </tr>
14        </tbody>
15    </table>
16</body>

JavaScript部分:

01<script type="text/javascript" src="http://www.zymseo.com/js/angular.min.js"></script>
02<script type="text/javascript">
03    var m = angular.module('app', []);
04     
05    m.service('zymBlog', ['$http'function($http){
06        this.get = function(){
07            return $http({
08                'url' './1.php',
09                'method' 'post'
10            }).then(function(result){
11                return result.data;
12            });
13        }
14    }]);
15     
16    m.controller('ctr', ['$scope''zymBlog'function($scope, zymBlog){
17        zymBlog.get().then(function(result){
18            $scope.data = result;
19        });
20    }]);
21</script>

PHP部分:

1<?php
2    header('Content-type:text/html;charset="utf-8"');
3     
4    $arry array(
5        array('webname'=>'赵一鸣技术博客''weburl'=>'http://www.zymseo.com'),
6        array('webname'=>'太原雅辉装修网''weburl'=>'http://www.0351zhuangxiu.com')
7    );
8     
9    echo json_encode($arry);

可以仔细对比一下service与factory自定义服务的区别!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值