根据我的理解,当我在工厂中返回一个被注入到控制器的对象。当在服务中,我使用这个处理对象,不返回任何东西。
我假设服务总是单例的,并且每个控制器中都会注入一个新的工厂对象。然而,正如事实证明的那样,工厂对象也是单例的吗?
演示的示例代码:
var factories = angular.module('app.factories', []);
var app = angular.module('app', ['ngResource', 'app.factories']);
factories.factory('User', function () {
return {
first: 'John',
last: 'Doe'
};
});
app.controller('ACtrl', function($scope, User) {
$scope.user = User;
});
app.controller('BCtrl', function($scope, User) {
$scope.user = User;
});
更改用户时。首先在ACtrl中,结果是那个用户。BCtrl中的first也改变了,例如User is a singleton?
我的假设是一个新的实例被注入到一个带有工厂的控制器中?
所有angular服务都是单例:
文档(请参阅服务为单例):https://docs.angularjs.org/guide/services
最后,重要的是要认识到所有Angular服务都是应用程序单例。这意味着每个注入器只有一个给定服务的实例。
基本上,服务和工厂的区别如下:
app.service('myService', function() {
// service is just a constructor function
// that will be called with 'new'
this.sayHello = function(name) {
return "Hi " + name + "!";
};
});
app.factory('myFactory', function() {
// factory returns an object
// you can run some code before
return {
sayHello : function(name) {
return "Hi " + name + "!";
}
}
});
查看关于$ provider的演示:http://slides.wesalvaro.com/20121113/#/
这些幻灯片被用于AngularJs的一个聚会:http://blog.angularjs.org/2012/11/more-angularjs-meetup-videos.html
对我来说,当我意识到它们都以相同的方式工作时,我得到了启示:通过运行一次,存储它们得到的值,然后在通过依赖注入引用时吐出相同的存储值。
假设我们有:
app.factory('a', fn);
app.service('b', fn);
app.provider('c', fn);
这三者的区别在于:
A的存储值来自运行fn,换句话说:fn()
B的存储值来自于new fn,换句话说:new fn()
C的存储值来自于首先通过new fn获得一个实例,然后运行实例的$get方法
这意味着,在angular内部有一个类似缓存对象的东西,它的每次注入的值只被赋值一次,当它们第一次被注入时,并且在:
cache.a = fn()
cache.b = new fn()
cache.c = (new fn()).$get()
这就是为什么我们在服务中使用This,并定义一个This。$get在供应商。
生活的例子
" Hello world "的例子
与工厂/服务/供应商:
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()];
}
所有angular服务都是单例:
文档(请参阅服务为单例):https://docs.angularjs.org/guide/services
最后,重要的是要认识到所有Angular服务都是应用程序单例。这意味着每个注入器只有一个给定服务的实例。
基本上,服务和工厂的区别如下:
app.service('myService', function() {
// service is just a constructor function
// that will be called with 'new'
this.sayHello = function(name) {
return "Hi " + name + "!";
};
});
app.factory('myFactory', function() {
// factory returns an object
// you can run some code before
return {
sayHello : function(name) {
return "Hi " + name + "!";
}
}
});
查看关于$ provider的演示:http://slides.wesalvaro.com/20121113/#/
这些幻灯片被用于AngularJs的一个聚会:http://blog.angularjs.org/2012/11/more-angularjs-meetup-videos.html