据我了解,它们几乎都是相同的。主要区别在于它们的复杂性。提供者可以在运行时进行配置,工厂要健壮一些,服务是最简单的形式。
看看这个问题AngularJS:服务vs提供者vs工厂
同样,这一要点可能有助于理解细微的差异。
来源:https://groups.google.com/forum/#!topic / angular / hVrkvaHGOfc
jsFiddle:http : //jsfiddle.net/pkozlowski_opensource/PxdSP/14/
作者:Pawel Kozlowski
var myApp = angular.module('myApp', []);
//service style, probably the simplest one
myApp.service('helloWorldFromService', function() {
this.sayHello = function() {
return "Hello, World!";
};
});
//factory style, more involved but more sophisticated
myApp.factory('helloWorldFromFactory', function() {
return {
sayHello: function() {
return "Hello, World!";
}
};
});
//provider style, full blown, configurable version
myApp.provider('helloWorld', function() {
// In the provider function, you cannot inject any
// service or factory. This can only be done at the
// "$get" method.
this.name = 'Default';
this.$get = function() {
var name = this.name;
return {
sayHello: function() {
return "Hello, " + name + "!";
}
};
};
this.setName = function(name) {
this.name = name;
};
});
//hey, we can configure a provider!
myApp.config(function(helloWorldProvider){
helloWorldProvider.setName('World');
});
function MyCtrl($scope, helloWorld, helloWorldFromFactory, helloWorldFromService) {
$scope.hellos = [
helloWorld.sayHello(),
helloWorldFromFactory.sayHello(),
helloWorldFromService.sayHello()];
}
Factories
(在上面引用)有点令人困惑。下面的一些答案Factories
甚至都可以理解为某些问题