1

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.

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.

1 Answer 1

1

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.

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks..I used your very first approach. Now I am not getting different errors like 1) "TypeError: Cannot read property 'get' of undefined" 2) "TypeError: apiservice.getTestData is not a function"
That will be to do with whatever you return from your .factory(apiservice', function() { ... }) function. If you update to show what service is when you return it, I'll take a look.
Yes but it started coming after I minified and bundled the files. The problem is its not recognizing the dependencies. The first error is coming on $http.get(). Its not finding $http, hence shouting 'get' of undefined.
If you use the ['$http', function($http) { }] syntax, that can't happen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.