AngularJS的自定义服务,factory、provider、service、constant、value等方法的区别

module
- filter() 自定义过滤器
- directive() 自定义指令
- factory() 自定义服务
- provider()
区别
$get

下面我们介绍一下factory()的自定义操作

<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>factory方法的自定义服务</title>
<script src="angular.min.js"></script>
<script>
var m1 = angular.module('myApp',[]);

//首先在模块m1下添加factory的方法,它接受两个参数,第一个参数是服务的名字,第二个参数是一个回调函数,可以是数组的形式也可以只是个回调函数,如果是要引入一些服务操作的话尽量使用数组的形式
m1.factory('myService',function(){
    return{//通过return的方式返回一个结果,例如写一个属性和一个方法
        name :‘hello’,
        show : function(){
            return this.name + ':angular';
        }
    };
});

m1.controller('Aaa',['$scope','myService',function($scope){
    console.log(myService.show());//在控制台打印出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.factory('myRandomNum',function(){
    return function(num1,num2){
        return Math.random()*(num2-num1)+num1;
    };
})

m1.controller('Aaa',['$scope','myRandomNum',function($scope,myRandomNum){
    console.log(myRandomNum(-3,6));
}]);

</script>
</head>
<body>
    <div ng-controller="Aaa"> 
    </div>
</body>
</html>

我们也可以通过provider的方法来自定义服务,provider和factory其实是比较类似的;
下面我们主要说一下factory与provider的区别;
factory方法是不能够进行初始化配置的,也就是我们所说的模块下面供应商,利用config方法来初始化操作是不可以的;但是如果我们自定义的服务想进行初始化配置,就需要使用provider的方法;
AngularJS供应商——初始化配置介绍

<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>provider</title>
<script src="angular.min.js"></script>
<script>
var m1 = angular.module('myApp',[]);
m1.provider('myRandomNum',function(){   //里面除了要写return之外还需要写一个$get的方法
    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(true);  //取整
}]);

m1.controller('Aaa',['$scope','myRandomNum',function($scope,myRandomNum){  
    console.log( myRandomNum(-3,6) );   
}]);
</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']);//引入module1
m2.controller('Aaa',['$scope','myService',function($scope,myService){    
    console.log(myService.show());  
}]);
</script>
</head>
<body>
<div ng-controller="Aaa">
</div>
</body>
</html>

factory与provider还有个区别就是factory可以多次调用;例如我们建一个service.js的文件
service.js

var m1 = angular.module('module1',[]);
m1.factory('myService',function(){
    return {
        name : 'hello',
        show : function(){
            return this.name + ':angular';
        }
    };
});

可以分别在两个页面调用
页面一

<!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 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>

注意事项:
1、自定义的服务,尽量不要用美元符号开头;因为以美元符号开始的都是内部服务
2、一般情况下自定义服务写在内部服务的后面

AngularJS除了factory与provider之外还有些不常用的自定义服务
模块间的通信
-service()
-针对面向对象的,构造函数的服务

<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>service服务</title>
<script src="angular.min.js"></script>
<script>
var m1 = angular.module('myApp',[]);
m1.service('myService',FnService);
function FnService(){//构造函数
    this.name = 'hello';
}
FnService.prototype.age = 20;
m1.controller('Aaa',['$scope','myService',function($scope,myService){    
    console.log(myService.name);
    console.log(myService.age); 
}]);
</script>
</head>
<body>
<div ng-controller="Aaa">  
</div>
</body>
</html>

constant()
-设置常量
constant和value都是用来设置常量的,他们的区别是constant方法是可以通过config方法配置获取的,而value方法是不可以通过配置获取的;

<!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.constant('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>

value()
- 区别

总结:
①factory方法是不能用config方法进行初始化配置的,而provider是可以使用config方法进行初始化配置的
②factory方法可以在多次调用
③service方法是针对面向对象,构造函数的自定义服务
④constant方法与value方法都是用来设置常量的,但是constant方法可以通过config方法进行初始化配置,而value方法不可以进行初始化配置

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值