与factory类似,AngularJs也可以通过service来自定义服务,只是在return返回数据的写法上稍有不同,具体代码如下:
HTML部分:
01 | <body ng-app= "app" ng-controller= "ctr" ng-cloak class= "ng-cloak" > |
02 | <table cellpadding= "0" cellspacing= "0" > |
10 | < tr ng-repeat= "(key, value) in data" > |
11 | <td>{{value.webname}}</td> |
12 | <td>{{value.weburl}}</td> |
|
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' , []); |
05 | m.service( 'zymBlog' , [ '$http' , function ($http){ |
06 | this .get = function (){ |
10 | }).then( function (result){ |
16 | m.controller( 'ctr' , [ '$scope' , 'zymBlog' , function ($scope, zymBlog){ |
17 | zymBlog.get().then( function (result){ |
|
PHP部分:
2 | header( 'Content-type:text/html;charset="utf-8"' ); |
9 | echo json_encode( $arry ); |
|
可以仔细对比一下service与factory自定义服务的区别!