本文共 610 字,大约阅读时间需要 2 分钟。
angular中的service叫做服务,用来封装常用的函数。就像java中的Util类一样。
以PathUtil服务为例,定义见下:
'use strict';/** * url的前缀固定,后缀会变,所以封装成服务,减少代码量 */angular.module('myApp') .service('PathUtil', function PathUtil() { var rootPath = 'http://127.1.2.3/CepWebservice/'; this.getUrl = function (relativeUrl) { return rootPath + relativeUrl; }; });
'use strict';angular.module('myApp')//自定义的PathUtil服务被当做单例传入 .controller('ChannelCtrl', function ($scope,$http,PathUtil,toastr) { $http .get(PathUtil.getUrl("channel")) .success(function(response) { $scope.channelList=response; toastr.success('频道配置拉取成功'); }); };);
转载地址:http://xxxpl.baihongyu.com/