<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script src="angular.min.js"></script> </head>
当你初试Angular 时,很自然地就会往controller 和scope 里堆满不必要的逻辑。一定要早点意识到,
controller 这一层应该很薄;也就是说,应用里大部分的业务逻辑和持久化数据都应该放在service
里。很多人问道,关于如何在controller 该干的事。出于内存性能的考虑,controller 只在需要
的时候才会初始化,一旦不需要就会被抛弃。因此,每次当你切换或刷新页面的时候,Angular 会清空当前的
controller。与此同时,service可以用来永久保存应用的数据,并且这些数据可以在不同的controller之间使用。
Angular 提供了3种方法来创建并注册我们自己的服务。1.Provider2.Factory3.Service
<body ng-app="myApp">
<div ng-controller="firstController">
{{name}}
</div>
<script>
var m1 = angular.module('myApp',[],function($provide){//通过angular.module的第三个参数配置服务
$provide.provider('providerServices01',function(){//providerServices01是服务的名称,第二个参数是函数,自定义的服务。
this.$get=function(){
return {
message:'this is providerServices01'
}
}
})
}); console.log(m1);
m1.controller('firstController',['$scope','providerServices01',function($scope,providerServices01){//在controller中使用服务
console.log(providerServices01);
$scope.name='张三';
}]);
</script>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script src="angular.min.js"></script> </head> <body ng-app="myApp">
<div ng-controller="firstController">
{{name}} </div>
<script>
var m1 = angular.module('myApp',[]);//module.config配置服务
m1.config(function($provide){
$provide.provider('providerServices01',function(){
this.$get=function(){//服务的方法
return {
message:'this is providerServices01'
}
}
}); $provide.provider('providerServices02',function(){
this.$get=function(){
var _name='';
var service={};
service.setName=function(name){
_name=name;
}
service.getName=function(){
return _name;
}
return service;
}
});
})
console.log(m1); m1.controller('firstController',['$scope','providerServices01','providerServices02',function($scope,providerServices01,providerServices02){
console.log(providerServices01);
providerServices02.setName('李四1111');
$scope.name= providerServices02.getName();
console.log( providerServices02);
}]); </script>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script src="angular.min.js"></script> </head>
//自定义服务
<body ng-app="myApp">
<div ng-controller="firstController">
{{name}}
</div>
<script>
var m1 = angular.module('myApp',[],function($provide){
$provide.provider('providerServices01',function(){
this.$get=function(){//需要get
return 'this is providerServices01';
}
});
$provide.factory('factoryServices01',function(){
return {//不需要$get
message:'this is factoryServices01'
}
});
$provide.factory('factoryServices02',function(){
return 'this is factoryServices01 text';
});
$provide.service('serviceServices01',function(){
return {
message:'this is serviceServices01'
}
})
});
console.log(m1); m1.controller('firstController',['$scope','providerServices01','factoryServices01','factoryServices02','serviceServices01',function($scope,providerServices01,factoryServices01,factoryServices02,serviceServices01){
console.log(providerServices01);
console.log(factoryServices01);
console.log(factoryServices02);
console.log(serviceServices01);
$scope.name='张三';
}]);
</script>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script src="angular.min.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="firstController">
{{name}}
</div>
<script>
var m1 = angular.module('myApp',[]);
m1.provider('providerServices01',function(){
this.$get=function(){
return {
message:'this is providerServices01'
}
}
});
m1.service('serviceServices01',function(){
return {
message:'this is serviceServices01'
}
})
console.log(m1);
m1.controller('firstController',['$scope','providerServices01','serviceServices01',function($scope,providerServices01,serviceServices01){
console.log(providerServices01);
console.log(serviceServices01);
$scope.name='张三';
}]);
</script>
</body>
</html>

angularjs1-6,自定义服务的更多相关文章

  1. angularJS1笔记-(4)-自定义服务

    html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

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

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

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

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

  4. angularJs自定义服务

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

  5. Angular JS 学习笔记(自定义服务:factory,Promise 模式异步请求查询:$http,过滤器用法filter,指令:directive)

    刚学没多久,作了一个小项目APP,微信企业号开发与微信服务号的开发,使用的是AngularJS开发,目前项目1.0版本已经完结,但是项目纯粹为了赶工,并没有发挥AngularJS的最大作用,这几天项目 ...

  6. Dynamics AX 2012 R2 在增强入站端口中找不到自定义服务操作

        Reinhard写好自定义服务A,添加好服务操作A1,A2,A3.....     然后,Reinhard在增强的入站端口,选择服务操作时,却找不到这些A1,A2,A3.     查找相关资料 ...

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

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

  8. Android系统在新进程中启动自定义服务过程(startService)的原理分析

    在编写Android应用程序时,我们一般将一些计算型的逻辑放在一个独立的进程来处理,这样主进程仍然可以流畅地响应界面事件,提高用户体验.Android系统为我们提供了一个Service类,我们可以实现 ...

  9. angular factory Services provider 自定义服务 工厂

    转载于 作者:海底苍鹰地址:http://blog.51yip.com/jsjquery/1602.html 1.在app.js 中声明了模块的依赖 var phonecatApp = angular ...

  10. 通过ModuleImplAdvertisement向自定义服务传递参数

    无意中发现通过ModuleImplAdvertisement可以向自定义服务传递参数,有空试一试. —————————————————————————————————————————————————— ...

随机推荐

  1. postgresql 常规操作以及检查备份

    一.建表时,复制源表的信息test=# test=# \d test.t1 Table "test.t1" Column | Type | Collation | Nullable ...

  2. AndroidStudio项目CMakeLists解析

    # For more information about using CMake with Android Studio, read the# documentation: https://d.and ...

  3. VS中的路径宏

    说明$(RemoteMachine)设置为“调试”属性页上“远程计算机”属性的值.有关更多信息,请参见更改用于 C/C++ 调试配置的项目设置.$(References)以分号分隔的引用列表被添加到项 ...

  4. Hadoop MapReduce编程 API入门系列之join(二十六)(未完)

    不多说,直接上代码. 天气记录数据库 Station ID Timestamp Temperature 气象站数据库 Station ID Station Name 气象站和天气记录合并之后的示意图如 ...

  5. SQLServer inner join,left join,right join,outer join 备忘备忘

    LEFT JOIN LEFT JOIN 关键字会从左表那里返回所有的行,即使在右表中没有匹配的行. 即LEFT JOIN 的 ON 条件不会对数据行造成影响 RIGHT JOIN RIGHT JOIN ...

  6. 【Oracle】DG中 Switchover 主、备切换

    操作系统:OEL 5.6 数据库版本:Oracle11gR2  11.2.0.4.0 Switchover切换要求主库和备库在数据同步情况下进行,是主备之间的正常切换,主要用于日常维护.灾备演练等.切 ...

  7. Python操作Oracle

    [root@oracle02 pythonsoftware]# rpm -ivh oracle-instantclient-basic-11.1.0.1-1.x86_64.rpm Preparing. ...

  8. 开启RxSwift之旅——开篇

    开启RxSwift之旅——开篇 RxSwift 是 ReactiveX 在 Swift 下的实现.ReactiveX 是一个通过使用可观察序列来组合异步和基于事件的程序的库. 很多地方通常把 Reac ...

  9. PHP 常用 数组函数

    1:array_push($arr,'添加的值') 往数组里面添加元素2:array_unique($arr) 去重函数3:array_reverse($arr) 倒叙排列

  10. Apex语言(二)变量与常量

    1.变量 凡是交给计算运算(处理)的数据就是变量,用来保存参加运算的数据和计算结果. 变量由变量名来标识. 变量名由字母数字和下划线组成,不能以数字开头. [正确]number,number1,num ...