<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script src="angular.min.js"></script>
<script>
var m1 = angular.module('myApp',[]); //自定义服务
m1.factory('myService',function(){//服务的名字,回调函数(跟controller语法一样,可以是数组)
return {
name : 'hello',
show : function(){
return this.name + ':angular';
}
};
});
m1.controller('Aaa',['$scope','myService',function($scope,myService){//使用myService服务(要形参传进去)
console.log(myService.show());
}]); m1.factory('myRandomNum',function(){
return function(num1,num2){
return Math.random()*(num2 - num1) + num1;
};
});
m1.controller('Aaa',['$scope','myRandomNum',function($scope,myRandomNum){
console.log( myRandomNum(-,) );
}]); m1.factory('myService',function(){
return {
name : 'hello',
show : function(){
return this.name + ':angular';
}
};
}); m1.provider('myService',function(){
return {
name : 'hello',
$get : function(){
return {
name : this.name,
show : function(){
return this.name + ':angular';
}
};
}
};
}); m1.config(['myServiceProvider',function(myServiceProvider){
console.log(myServiceProvider);
myServiceProvider.name = 'hi';
}]); m1.controller('Aaa',['$scope','myService',function($scope,myService){
console.log(myService.show());
}]); m1.provider('myRandomNum',function(){
return {
bolInt : false,
int : function(argBol){
if(argBol){
this.bolInt = true;
}
else{
this.bolInt = false;
}
},
$get : function(){
var This = this;
return function(num1,num2){
return This.bolInt ? Math.round(Math.random()*(num2 - num1)) + num1 : Math.random()*(num2 - num1) + num1;
};
}
};
}); m1.config(['myRandomNumProvider',function(myRandomNumProvider){
myRandomNumProvider.int(false);
}]); m1.controller('Aaa',['$scope','myRandomNum',function($scope,myRandomNum){
console.log( myRandomNum(-,) );
}]); </script>
</head> <body>
<div ng-controller="Aaa"> </div>
</body>
</html>
<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script src="angular.min.js"></script>
<script> var m1 = angular.module('module1',[]);
m1.factory('myService',function(){
return {
name : 'hello',
show : function(){
return this.name + ':angular';
}
};
}); var m2 = angular.module('myApp',['module1']);
m2.controller('Aaa',['$scope','myService',function($scope,myService){
console.log(myService.show());
}]); </script>
</head> <body>
<div ng-controller="Aaa"> </div>
</body>
</html>
<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script src="angular.min.js"></script>
<script src="service.js"></script>
<script> var m2 = angular.module('myApp',['module1']); m2.controller('Aaa',['$scope','myService',function($scope,myService){ console.log(myService.show()); }]); </script>
</head> <body>
<div ng-controller="Aaa"> </div>
</body>
</html>
<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script src="angular.min.js"></script>
<script src="service.js"></script>
<script> var m2 = angular.module('myApp',['module1']); m1.config(['myServiceProvider',function(myServiceProvider){ myServiceProvider.name = 'hi'; }]); m2.controller('Aaa',['$scope','myService',function($scope,myService){ console.log(myService.show()); }]); </script>
</head> <body>
<div ng-controller="Aaa"> </div>
</body>
</html>
<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script src="angular.min.js"></script>
<script> var m1 = angular.module('myApp',[]);
m1.service('myService',FnService);//FnService是回调函数(也可以使数组[]),跟controller语法一样,
function FnService(){
this.name = 'hello';
}
FnService.prototype.age = ; m1.controller('Aaa',['$scope','myService',function($scope,myService){
console.log(myService.name);
console.log(myService.age);
}]); var m1 = angular.module('myApp',[]); m1.constant('myService','hello angular');
//m1.value('myService','hello angular');
m1.config(['myService',function(myService){
console.log(myService);
}]); m1.controller('Aaa',['$scope','myService',function($scope,myService){ console.log(myService); }]); </script>
</head> <body>
<div ng-controller="Aaa"> </div>
</body>
</html>

angularjs 自定义服务的更多相关文章

  1. angularjs 自定义服务的三种方式

    angularjs 中可通过三种($provider,$factory,$service)方式自定义服务,以下是不同的实现形式: // 定义module , module中注入$providevar ...

  2. angularJs自定义服务(实现签名和加密)

    写在前面: angularJS是google公司主推的js开发优秀框架... 页面展示: 在应用中进行加密是普遍存在的,个人建议在前端实现加密签名(前端加密是否必要来自知乎:http://www.zh ...

  3. angularJs自定义服务

    在AngularJS中,系统内置的服务都是以$开头,所以我们的自定义服务尽量避免以$开头.自定义服务的方式有如下几种: 使用Module的provider方法 使用Module的factory方法 使 ...

  4. 深究AngularJS——自定义服务详解(factory、service、provider)

    前言 3种创建自定义服务的方式.  Factory Service Provider 大家应该知道,AngularJS是后台人员在工作之余发明的,他主要应用了后台早就存在的分层思想.所以我们得了解下分 ...

  5. angularJs 自定义服务 provide 与 factory 的区别

    <!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> <met ...

  6. angularjs 自定义服务(serive,factory,provder) 以及三者的区别

    1.Serive 服务:通过service方式创建自定义服务,相当于new的一个对象:var s = new myService();,只要把属性和方法添加到this上才可以在controller里调 ...

  7. 42.angularJS自定义服务

    转自:https://www.cnblogs.com/best/tag/Angular/ 1. 你可以创建自定义服务,链接到你的模块中: <!DOCTYPE html> <html& ...

  8. angularJS自定义服务的几种方式

    在angularJS中定义服务共有四种常见的方式:factory,service,provider,constant,value 使用形式的不同: 1)factory以返回对象的形式定义服务: mya ...

  9. angularJS——自定义服务provider之$get

    可以认为provider有三个部分: 第一部分是私有变量和私有函数,这些变量和函数会在以后被修改. 第二部分是在app.config函数里可以访问的变量和函数,所以,他们可以在其他地方使用之前被修改. ...

随机推荐

  1. 微信企业号开发:微信用户信息和web网页的session的关系

         微信企业号的用户是须要验证的,因此能关注企业号的用户事实上就是已经通过验证的用户.但企业应用中打开一个网页,在这个网页中怎样依据微信用户的信息创建web应用中最长使用的session呢?微信 ...

  2. Android 中文字体的设置方法和使用技巧

    Android TextView字体颜色等样式具体解释连接:http://blog.csdn.net/pcaxb/article/details/47341249 1.使用字体库(自己定义字体的使用) ...

  3. 前端project师养成记:开发环境搭建(Sublime Text必备插件推荐)

    为了让自己更像一个前端project师,决定从开发环境開始武装自己. 本文将介绍前段project师开发的一些利器的安装步骤,主要包含了: 1.Node.js的安装 2.Grunt的安装及经常使用插件 ...

  4. Codeforces 10A-Power Consumption Calculation(模拟)

    A. Power Consumption Calculation time limit per test 1 second memory limit per test 256 megabytes in ...

  5. 算法 - 求一个数组的最长递减子序列(C++)

    //************************************************************************************************** ...

  6. 1.RunLoop是什么?

    1.Run loops是线程相关的的基础框架的一部分. 一个run loop就是一个事件处理的循环.用来不停的调度工作以及处理输入事件.使用run loop的目的是让你的线程在有工作的时候忙于工作.而 ...

  7. bzoj2150: 部落战争(匈牙利)

    2150: 部落战争 题目:传送门 题解: 辣鸡数据..毁我AC率 先说做法,很容易就可以看出是二分图匹配的最小路径覆盖(可能是之前不久刚做过类似的题) 一开始还傻逼逼的去直接连边然后准备跑floyd ...

  8. Linux下安装ipython与jupyter

    IPython从Python发展而来,更倾向于科学计算.互联网数据分析更喜欢用. 首先切换root用户: su - root pip3自动安装ipython [root@hear ~]# ipytho ...

  9. 配置NTP集群时间同步(二)

    [root@hadoop1 bin]# rpm -qa|grep ntp 没有的话用yum -y install ntp安装(要记着每台机器上都要安装) [root@hadoop1 bin]# vi ...

  10. MapReduce 程序:WordCount