-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path40-自定义服务-provider.html
55 lines (49 loc) · 1.26 KB
/
40-自定义服务-provider.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>自定义服务</title>
</head>
<body>
<div ng-controller="test" id="parent" >
</div>
<script type="text/javascript" src="js/angular.min.js" ></script>
<script>
var m1 = angular.module('myApp',[]);
//创建自定义服务
/*
m1.factory('myService',function(){
return{
name:'hello',
show:function(){
return this.name +'angular';
}
}
});
*/
//用provider定义的自定义服务可以进行配置
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';
}]);
//自定义服务的调用有三个点:1.服务是已经定义好的 2.自定义服务的依赖顺序必须写在系统服务的后面 3.自定义服务起名时不需要加$
m1.controller('test',['$scope','myService',function($scope,myService){
console.log(myService.show());
console.log(myService.name);
}]);
</script>
</body>
</html>