<!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. 高级函数-decode

    decode(字段或计算表达式,           条件值1,结果值1,           条件值2,结果值2[,默认值]           )      if(字段或计算表达式 == 条件值1 ...

  2. Nginx 项目部署和配置

    nginx 作为代理服务器,需要代理多个项目的话配置如下: server { listen       80; server_name  localhost; #charset koi8-r; #ac ...

  3. COGS——T 1168. 机器调度

    http://www.cogs.pro/cogs/problem/problem.php?pid=1168 ★★   输入文件:machine.in   输出文件:machine.out   简单对比 ...

  4. HDU 4035

    dp求期望的题. 设 E[i]表示在结点i处,要走出迷宫所要走的边数的期望.E[1]即为所求. 叶子结点: E[i] = ki*E[1] + ei*0 + (1-ki-ei)*(E[father[i] ...

  5. sql两个字段相加减,第三个字段没有值的原因.

    错误的写法:(in_story_num-out_story_num) as story_num 正确的写法:(nvl(in_story_num,0)-nvl(out_story_num,0)) as ...

  6. CoreData(数据库升级 )版本迁移-iOS App升级安装

    版权声明:本文为博主原创文章,未经博主允许不得转载. 如果IOS App 使用到CoreData,并且在上一个版本上有数据库更新(新增表.字段等操作),那在覆盖安装程序时就要进行CoreData数据库 ...

  7. iOS学习必须了解的七大手势

    文章只要你有一点点基础应该就可以看的懂,文章只为学习交流 #import "ViewController.h" @interface ViewController () @prop ...

  8. NOIP卡常数技巧

    NOIP卡常数技巧 https://blog.csdn.net/a1351937368/article/details/78162078 http://www.mamicode.com/info-de ...

  9. BZOJ 2127 二元关系

    题意: 思路: 先把所有的值加起来 最小割割哪儿 就代表那个地方不选 一减 剩下的就是 最大值了 //By SiriusRen #include <cstdio> #include < ...

  10. 752. [BJOI2006] 狼抓兔子

    ★★★   输入文件:bjrabbit.in   输出文件:bjrabbit.out   简单对比时间限制:1 s   内存限制:162 MB Description   Source: Beijin ...