服务和工厂之间的区别
1)服务
服务是 constructor 函数,在运行时使用 new 调用一次,就像我们使用普通 javascript 所做的那样,只是 AngularJs 在幕后调用 new。
在服务的情况下,有一个拇指规则需要记住
- 服务是使用
new调用的构造函数
让我们看一个简单的例子,我们将注册一个使用 $http 服务获取学生详细信息的服务,并在控制器中使用它
function StudentDetailsService($http) {
this.getStudentDetails = function getStudentDetails() {
return $http.get('/details');
};
}
angular.module('myapp').service('StudentDetailsService', StudentDetailsService);
我们只是将此服务注入控制器
function StudentController(StudentDetailsService) {
StudentDetailsService.getStudentDetails().then(function (response) {
// handle response
});
}
angular.module('app').controller('StudentController', StudentController);
什么时候用?
在任何想要使用构造函数的地方使用 .service()。它通常用于创建公共 API,就像 getStudentDetails() 一样。但是如果你不想使用构造函数并希望使用简单的 API 模式,那么 .service() 就没有太大的灵活性了。
2)工厂
虽然我们可以使用 .factory() 来实现所有的东西,但是使用 .services(),它并不能使 .factory() 与“.service()”相同。它比 .service() 更强大,更灵活
.factory() 是一种用于返回值的设计模式。
在工厂的情况下,要记住两个拇指规则
- 工厂返回值
- 工厂(可以)创建对象(任何对象)
让我们看看使用 .factory() 可以做些什么的一些例子
返回对象文字
让我们看一个示例,其中工厂使用基本的 Revealing 模块模式返回对象
function StudentDetailsService($http) {
function getStudentDetails() {
return $http.get('/details');
}
return {
getStudentDetails: getStudentDetails
};
}
angular.module('myapp').factory('StudentDetailsService', StudentDetailsService);
在控制器内使用
function StudentController(StudentDetailsService) {
StudentDetailsService.getStudentDetails().then(function (response) {
// handle response
});
}
angular.module('app').controller('StudentController', StudentController);
返回闭包
什么是关闭?
闭包是指在本地使用的变量的函数,但是在封闭范围内定义。
以下是一个闭包的例子
function closureFunction(name) {
function innerClosureFunction(age) { // innerClosureFunction() is the inner function, a closure
// Here you can manipulate 'age' AND 'name' variables both
};
};
在 精彩 的部分是,它可以访问 name 这是在父范围。
让我们在 .factory() 中使用上面的闭包示例
function StudentDetailsService($http) {
function closureFunction(name) {
function innerClosureFunction(age) {
// Here you can manipulate 'age' AND 'name' variables
};
};
};
angular.module('myapp').factory('StudentDetailsService', StudentDetailsService);
在控制器内使用
function StudentController(StudentDetailsService) {
var myClosure = StudentDetailsService('Student Name'); // This now HAS the innerClosureFunction()
var callMyClosure = myClosure(24); // This calls the innerClosureFunction()
};
angular.module('app').controller('StudentController', StudentController);
创建构造函数/实例
如上所示,.service() 创建了一个调用 new 的构造函数。.factory() 也可以创建一个调用 new 的构造函数
让我们看一个如何实现这一目标的例子
function StudentDetailsService($http) {
function Student() {
this.age = function () {
return 'This is my age';
};
}
Student.prototype.address = function () {
return 'This is my address';
};
return Student;
};
angular.module('myapp').factory('StudentDetailsService', StudentDetailsService);
在控制器内使用
function StudentController(StudentDetailsService) {
var newStudent = new StudentDetailsService();
//Now the instance has been created. Its properties can be accessed.
newStudent.age();
newStudent.address();
};
angular.module('app').controller('StudentController', StudentController);