[英]injecting dependency through factory in AngularJs
I have below service code 我有以下服务代码
(function () {
'use strict';
angular.module('App')
.factory('apiservice', apiservice);
/* @ngInject */
function apiservice($http) {
var service = {
getData: getGridData,
};
return service;
//code ommitted for clarity
}
})();
When I minifying and bundling, the dependency is getting screwed up. 当我缩小和捆绑在一起时,依赖关系就搞砸了。 So I wanted to change $scope to '$scope' so minifier won't mess up with name. 因此,我想将$ scope更改为 '$ scope',以便缩小器不会弄乱名称。
I know there are several ways to do it. 我知道有几种方法可以做到。 But can I make it work like below: 但是我可以使其工作如下:
(function () {
'use strict';
angular.module('App')
.factory('apiservice', ['http', apiservice]);
function apiservice($http) {
return service;
}
})();
Any help is appreciated. 任何帮助表示赞赏。
You have a couple of options,. 您有两种选择。 the first is to use the extended syntax for dependency injection. 首先是使用扩展语法进行依赖项注入。 In this case: 在这种情况下:
.factory('apiservice', ['$http', apiservice]);
function apiservice($http) {
return service;
}
This can also be written as: 也可以写成:
.factory('apiservice', apiservice);
apiservice.$inject = ['$http'];
function apiservice($http) {
return service;
}
Or put add another build step before you minify, such as ng-annotate which will convert your syntax to the extended version. 或在最小化之前添加另一个构建步骤,例如ng-annotate ,它将把您的语法转换为扩展版本。
A factory like this 这样的工厂
.factory('apiservice', function($http) {
return service;
});
Would become: 会成为:
.factory('apiservice', ['$http', function($http) {
return service;
});
Which would be safe to minify. 可以放心使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.