<!DOCTYPE html>
<html>
<script src="http://apps.bdimg.com/libs/angular.js/1.3.9/angular.min.js"></script>
<body>
<title>services</title><div ng-app="MyApp" ng-controller="MyController">
{{‘v1:‘+v1+‘ v2:‘+v2}}
</div>
<script type="text/javascript">angular.module(‘MyApp‘, [])
.service(‘s1‘, function(){
this.compare = function(a, b){
return a === b ? 0 : (a-b) / Math.abs(a-b);
};
})
.factory(‘s2‘, function(){
return {
a: 14,
b: 22
};
})
.provider(‘s3‘, function(){
this.$get = function(){ //$get
return {a:2,b:2};
};
})
.controller(‘MyController‘, function($scope, s1, s2, s3){
$scope.v1 = s1.compare(s2.a, s2.b);
$scope.v2 = s1.compare(s3.a, s3.b);
});
</script>
</body>
</html>
原文:http://www.cnblogs.com/w-rudolph/p/4951170.html