angularjs 自定义服务
<!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 自定义服务的更多相关文章
- angularjs 自定义服务的三种方式
angularjs 中可通过三种($provider,$factory,$service)方式自定义服务,以下是不同的实现形式: // 定义module , module中注入$providevar ...
- angularJs自定义服务(实现签名和加密)
写在前面: angularJS是google公司主推的js开发优秀框架... 页面展示: 在应用中进行加密是普遍存在的,个人建议在前端实现加密签名(前端加密是否必要来自知乎:http://www.zh ...
- angularJs自定义服务
在AngularJS中,系统内置的服务都是以$开头,所以我们的自定义服务尽量避免以$开头.自定义服务的方式有如下几种: 使用Module的provider方法 使用Module的factory方法 使 ...
- 深究AngularJS——自定义服务详解(factory、service、provider)
前言 3种创建自定义服务的方式. Factory Service Provider 大家应该知道,AngularJS是后台人员在工作之余发明的,他主要应用了后台早就存在的分层思想.所以我们得了解下分 ...
- angularJs 自定义服务 provide 与 factory 的区别
<!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> <met ...
- angularjs 自定义服务(serive,factory,provder) 以及三者的区别
1.Serive 服务:通过service方式创建自定义服务,相当于new的一个对象:var s = new myService();,只要把属性和方法添加到this上才可以在controller里调 ...
- 42.angularJS自定义服务
转自:https://www.cnblogs.com/best/tag/Angular/ 1. 你可以创建自定义服务,链接到你的模块中: <!DOCTYPE html> <html& ...
- angularJS自定义服务的几种方式
在angularJS中定义服务共有四种常见的方式:factory,service,provider,constant,value 使用形式的不同: 1)factory以返回对象的形式定义服务: mya ...
- angularJS——自定义服务provider之$get
可以认为provider有三个部分: 第一部分是私有变量和私有函数,这些变量和函数会在以后被修改. 第二部分是在app.config函数里可以访问的变量和函数,所以,他们可以在其他地方使用之前被修改. ...
随机推荐
- Ubuntu安装keepalived
Ubuntu安装keepalived 一.Keepalived是什么鬼东西: keepalived是集群管理中保证集群高可用的一个服务软件,其功能类似于heartbeat,用来防止单点故障. 二.Ke ...
- C++中对字符串进行插入、替换、删除操作
#include <iostream> #include <string> using std::cout; using std::endl; using std::strin ...
- SQL语句将某字段查询出以逗号隔开
MySQL的sql语句有好多能够省去server端的复杂处理 1.group_concat 这玩意儿能够实现 将一个字段如id查询出来 成为这种格式:121,122,123,124,125,12 ...
- perl getopt 用法
我们在linux经常常使用到一个程序须要增加參数,如今了解一下perl中的有关控制參数的函数.getopt.在linux有的參数有二种形式.一种是--help,还有一种是-h.也就是-和--的分别.- ...
- PHP第八课 字符串拆分经常使用函数
课程概要: 通过这节课可以对字符串进行主要的操作. 字符串知识点: 1.字符串的处理介绍 2.经常使用的字符串输出函数 3.经常使用的字符串格式化函数 4.字符串比較函数 5.正則表達式在字符串中的应 ...
- DNS Tunnel判定方法
DNS Tunnel判定方法: 1.查询DNS请求的域名是否存在备案: 2.查询DNS请求的域名情报信息(以及域名的alex排名): 3.查看相同主域名下子域名编码格式及长度:(存在Base32和Ba ...
- (六)api网关服务 zuul-过滤器
开启上文服务: Zuul给我们的第一印象通常是这样:它包含了对请求的路由和过滤两个功能,其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础.过滤器功能则负责对请求的处理过 ...
- MFC补码原码反码转换工具
/*_TCHAR str[100] = { 0 }; wsprintf(str, _T("%d"),num);*/ ; CString str; m_edit1.GetWindow ...
- Ubuntu16.04+OpenCV3.2.0+Opencv_Contrib3.2.0安装
为了学习slam,在ubuntu16.04系统上安装opencv3.2.0以及对应的opencv_contrib3.2.0 安装过程 下载 Github上下载有的时候比较慢,我这里分享了OpenCV3 ...
- 第三方库requests
requests库 # 1.记得安装 第三方 模块 requests # pip install requests import requests url = 'http://www.baidu.co ...