DeborahK DeborahK - 1 month ago 6
AngularJS Question

How do I use $inject when passing a function into an Angular .factory method?

My original code looked like this (see below). Note the $inject statement.

module app.common {
interface IProductResource
extends ng.resource.IResource<app.domain.IProduct> {

}
export class ResourceFactory {

static $inject =["$resource"];
constructor(private $resource: ng.resource.IResourceService) {
}

public getProductResource() : ng.resource.IResourceClass<IProductResource> {
return this.$resource("/api/products/:productId");
}
}

factory.$inject = ["$resource"];
function factory($resource) : ResourceFactory {
return new ResourceFactory($resource);
}

angular
.module("common.services")
.factory("resourceFactory",
["$resource",
factory]);
}


My new code leverages the lambda syntax and looks like this (see below). How do I handle the minification issues in this case?

module app.common {
interface IProductResource
extends ng.resource.IResource<app.domain.IProduct> {

}

export class ResourceFactory {

static $inject =["$resource"];
constructor(private $resource: ng.resource.IResourceService) {
}

public getProductResource() : ng.resource.IResourceClass<IProductResource> {
return this.$resource("/api/products/:productId");
}
}

angular
.module("common.services")
.factory("resourceFactory",
["$resource",
($resource)=>new ResourceFactory($resource)]);

}

Answer Source

To be honest you should do:

 angular
        .module("common.services")
        .service("resourceFactory",ResourceFactory);

Because of the use of static $inject and service it will just work.

More : https://www.youtube.com/watch?v=Yis8m3BdnEM