Angular-service.js是做什么用的?
- 内容介绍
- 文章标签
- 相关推荐
本文共计279个文字,预计阅读时间需要2分钟。
javascript// 使用:引入此JS,再使用angular.module('..', ['sfService']),然后在Controller中注入'sfHttp',例如:// controller('..', function(sfHttp) {});
var _sfService=angular.module('sfService', []);_sfService.factory('sfHttp', []);
angular-service.js/** * 使用:引入该JS,再angular.module('..',['sfService']) * 然后在Controller中注入'sfHttp' * 例子:controller('..',function (sfHttp){}); */ var _sfService = angular.module('sfService',[]); _sfService.factory('sfHttp',['$10.202.78.5:8080/vishnu-web';//为以后跨前后端分离留接口 _sfService.remotePath = '10.202.78.5:8080/vishnu-web'; //获取项目路径 _sfService.getRootPath = function () { if (_sfService.rootPath) { return _sfService.rootPath; } else { return _sfService.rootPath = (window.document.location.href.substring(0, window.document.location.href .indexOf(window.document.location.pathname)) + window.document.location.pathname .substring(0, window.document.location.pathname .substr(1).indexOf('/') + 1)); } } _sfService.provider('vishnu',[function () {//能注入config()的参数中去,例子:vishnuProvider this.rootPath = _sfService.getRootPath(); this.sayTime = new Date(); this.sayHello = function (name) { return 'Hello '+name; } this.$get = function ($http) { return { 'rootPath':this.rootPath, 'sayTime':this.sayTime, 'sayHello':this.sayHello, 'sfHttp':function (data) {//在config中不存在该函数,在run等方法中存在 $http({ method: 'GET', params: data.data, url: data.url }).then(function (result) { if (typeof data.success === "function") { if (result.data.data) { data.success(result.data.data, result); } else if (result.data) { data.success(result.data, result); } else { data.success(result); } } }, function (errMsg) { typeof data.error === "function" && data.error(errMsg); }); }, }; } }]); _sfService.provider('vishnu2',{//不能注入config中去,可以注入controller,如vishnu2 $get:function () { return { sayTime:new Date(), sayHello:'Hello World Angular!' } } })
本文共计279个文字,预计阅读时间需要2分钟。
javascript// 使用:引入此JS,再使用angular.module('..', ['sfService']),然后在Controller中注入'sfHttp',例如:// controller('..', function(sfHttp) {});
var _sfService=angular.module('sfService', []);_sfService.factory('sfHttp', []);
angular-service.js/** * 使用:引入该JS,再angular.module('..',['sfService']) * 然后在Controller中注入'sfHttp' * 例子:controller('..',function (sfHttp){}); */ var _sfService = angular.module('sfService',[]); _sfService.factory('sfHttp',['$10.202.78.5:8080/vishnu-web';//为以后跨前后端分离留接口 _sfService.remotePath = '10.202.78.5:8080/vishnu-web'; //获取项目路径 _sfService.getRootPath = function () { if (_sfService.rootPath) { return _sfService.rootPath; } else { return _sfService.rootPath = (window.document.location.href.substring(0, window.document.location.href .indexOf(window.document.location.pathname)) + window.document.location.pathname .substring(0, window.document.location.pathname .substr(1).indexOf('/') + 1)); } } _sfService.provider('vishnu',[function () {//能注入config()的参数中去,例子:vishnuProvider this.rootPath = _sfService.getRootPath(); this.sayTime = new Date(); this.sayHello = function (name) { return 'Hello '+name; } this.$get = function ($http) { return { 'rootPath':this.rootPath, 'sayTime':this.sayTime, 'sayHello':this.sayHello, 'sfHttp':function (data) {//在config中不存在该函数,在run等方法中存在 $http({ method: 'GET', params: data.data, url: data.url }).then(function (result) { if (typeof data.success === "function") { if (result.data.data) { data.success(result.data.data, result); } else if (result.data) { data.success(result.data, result); } else { data.success(result); } } }, function (errMsg) { typeof data.error === "function" && data.error(errMsg); }); }, }; } }]); _sfService.provider('vishnu2',{//不能注入config中去,可以注入controller,如vishnu2 $get:function () { return { sayTime:new Date(), sayHello:'Hello World Angular!' } } })

