acrmuui
acrmuui

Reputation: 2070

AngularJS: Correct minify-able syntax when using resolve with controllers

I'm using the resolve functionality with a couple of controllers to fetch some data before the rendering of a new view kicks in like this:

HomeCtrl.resolve = {
    pictures: function(Picture) {
        return Picture.getall();
    } 
};

How do I write this so the Picture-service, that is passed as an argument, doesn't get overwritten when minified?

Upvotes: 36

Views: 5612

Answers (2)

dlporter98
dlporter98

Reputation: 1630

Another way to make resolve function minification safe:

HomeCtrl.resolve = {
    pictures: getPictures
};

getPictures.$inject = ['Picture'];
function getPictures(Picture){
    return Picture.getall();
}

This technique takes advantage of the fact that in javascript functions are also objects and, therefore, can have properties.

For more information, go to https://docs.angularjs.org/guide/di and search for "$inject".

Upvotes: 2

Arun P Johny
Arun P Johny

Reputation: 388406

You can inject the dependencies using the following pattern, it is minification proof

HomeCtrl.resolve = {
    pictures : ['Picture', function(Picture) {
                return Picture.getall();
            }]
};

Upvotes: 54

Related Questions