【发布时间】:2015-04-17 04:47:30
【问题描述】:
我正在使用 service 应该有 factory 并且我没有收到错误。
我的服务在应该返回构造函数时返回和对象。
但是我的代码运行良好。
例如,下面的代码应该不起作用。但它工作正常:
angular.module('myApp', [])
.service('myService', myService);
function myService(/*<dependencies go in here>*/){
return {
/*<object attributes in here>*/
}
}
正确的代码应该是这样的:
angular.module('myApp', [])
.factory('myService', myService);
function myService(/*<dependencies go in here>*/){
return {
/*<object attributes in here>*/
}
}
我的问题是为什么 Angular 允许您在实际返回对象时使用 .service 并且应该使用 .factory
【问题讨论】:
-
这是一个有趣的问题
标签: javascript angularjs angularjs-service