Skip to content

Instantly share code, notes, and snippets.

@polinagogo
Last active March 14, 2017 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save polinagogo/ebeb695992289c1b74eb682d78f94893 to your computer and use it in GitHub Desktop.
Save polinagogo/ebeb695992289c1b74eb682d78f94893 to your computer and use it in GitHub Desktop.
Making sure we have minification safe angular code

Instead of doing this:

export default function (campaignService) {
  return {
    scope: {},
    template: require('./my-directive.html'),
    link: function(scope) {
      campaignService.getCampaigns().then((campaigns) => {
        scope.campaigns = campaigns;
      });
    }
  };
}

Name your functions!

export default function myDirective(campaignService) {
  return {
    scope: {},
    template: require('./my-directive.html'),
    link: function(scope) {
      campaignService.getCampaigns().then((campaigns) => {
        scope.campaigns = campaigns;
      });
    }
  };
}

Instead of annotating like this (which is not going to work):

/*@ngInject*/
import "./styles.less";
export default function myDirective(campaignService) {
  return {
    scope: {},
    template: require('./my-directive.html'),
    link: function(scope) {
      campaignService.getCampaigns().then((campaigns) => {
        scope.campaigns = campaigns;
      });
    }
  };
}

Annotate like this:

import "./styles.less";
export default function myDirective(campaignService) {
 "ngInject"; //I am a string literal
  return {
    scope: {},
    template: require('./my-directive.html'),
    link: function(scope) {
      campaignService.getCampaigns().then((campaigns) => {
        scope.campaigns = campaigns;
      });
    }
  };
}

ng-annotate will then use ngInject to add the following in order to ensure that our Angular code is minification safe:

import "./styles.less";
myDirective.$inject = ['$campaignService'];
export default function myDirective(campaignService) {
 "ngInject"; //I am a string literal
  return {
    scope: {},
    template: require('./my-directive.html'),
    link: function(scope) {
      campaignService.getCampaigns().then((campaigns) => {
        scope.campaigns = campaigns;
      });
    }
  };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment