-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path08.factory-service-provider.js
137 lines (111 loc) · 3.1 KB
/
08.factory-service-provider.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var app = angular.module("app", []);
app.controller("emp", ["$scope", "calcProv", function($scope, calcProv){
$scope.a = 10;
$scope.b = 20;
$scope.doSum = function(){
// 1. synchornous way
//$scope.sum = calcFactory.getSum($scope.a, $scope.b);
//$scope.sum = calcService.getSum($scope.a, $scope.b);
//$scope.sum = calcProv.getSum($scope.a, $scope.b);
// 2. using callback(asynchronous way)
// calcFactory.getSum($scope.a, $scope.b, function(result){
// $scope.sum = result;
// });
// calcService.getSum($scope.a, $scope.b, function(result){
// $scope.sum = result;
// });
calcProv.getSum($scope.a, $scope.b, function(result){
$scope.sum = result;
});
};
}]);
// example of Factory
app.factory("calcFactory", ["$http", "$log", function($http, $log){
$log.log("Instantiating Factory....");
// var oCalcfactory = {};
// // 1. synchornous way
// oCalcfactory.getSum = function(a, b){
// return parseInt(a) + parseInt(b);
// };
// // 2. using callback(asynchronous way)
// oCalcfactory.getSum = function(a, b, cb){
// var s = parseInt(a) + parseInt(b);
// cb(s);
// };
// // 3. web-service
// oCalcfactory.getSum = function(a, b, cb){
// $http({
// url: 'http://localhost:3000/sum?a=' + a + '&b=' + b,
// method: "GET"
// }).then(function(resp){
// // sucess msg will go here
// $log.log(resp.data);
// cb(resp.data);
// },function(resp){
// // failour msg will go here
// $log.log(resp.data);
// });
// };
// return oCalcfactory;
}]);
// example of Service
app.service("calcService", ["$http", "$log", function($http, $log){
$log.log("Instantiating Service....");
// // 1. synchornous way
// this.getSum = function(a, b){
// return parseInt(a) + parseInt(b);
// };
// // 2. using callback(asynchronous way)
// this.getSum = function(a, b, cb){
// var s = parseInt(a) + parseInt(b);
// cb(s);
// };
// // 3. web-service
// this.getSum = function(a, b, cb){
// $http({
// url: 'http://localhost:3000/sum?a=' + a + '&b=' + b,
// method: "GET"
// }).then(function(resp){
// // sucess msg will go here
// $log.log(resp.data);
// cb(resp.data);
// },function(resp){
// // failour msg will go here
// $log.log(resp.data);
// });
// };
}]);
// example of Provider
app.provider("calcProv", function(){
var baseUrl = '';
this.config = function(url){
baseUrl = url;
};
this.$get = ["$log", "$http", function($log, $http){
$log.log("Instantiating Provider....");
var objCalcProv = {};
// 1. synchornous way
// objCalcProv.getSum = function(a, b){
// return parseInt(a) + parseInt(b);
// };
// 2. using callback(asynchronous way)
// objCalcProv.getSum = function(a, b, cb){
// cb(parseInt(a)+parseInt(b));
// };
// 3. web-service
objCalcProv.getSum = function(a, b, cb) {
$http({
url: baseUrl + 'sum?a=' + a + '&b=' + b,
method: 'GET'
}).then(function(sucess){
cb(sucess.data);
},function(error){
$log.error(error.data);
});
};
return objCalcProv;
}];
});
app.config(["calcProvProvider", function(calcProvProvider){
calcProvProvider.config('http://localhost:3000/');
}]);