1

I'm creating an AngularJS module which contains some directives that will be reused in other projects within my organization. Currently, when some project needs to use this generic module, I need to send two things: the javascript with the AngularJS code where all the directives are created and all the template views that my directives use.

My current structure is like the following:

var commonWidgets = angular.module('commonWidgets', []);

commonWidgets.directive('commonGrid', function () {
    return {
        restrict: 'A',
        scope: {
            options: '='
        },
        templateUrl: '/views/grid.html',
        link: function (scope) {
           ...
        }

    };
});

In this example I have to share common-widgets.js and /views/grid.html with the other project.

Question: Is there a way that I can send only the javascript code?

One possible solution that I tought would be declaring my javascript code like the following:

var commonWidgets = angular.module('commonWidgets', []);

commonWidgets.directive('commonGrid', function () {
    return {
        restrict: 'A',
        scope: {
            options: '='
        },
        template: '<div>A relative big code goes here</div>',
        link: function (scope) {
           ...
        }

    };
});

The issue is that when I have the template in the Javascript code, it is difficult to maintain. In the other hand, the consumer of this library does not need to worry about the views.

Is there a way that I can change templateUrl to template while building a production-ready AngularJS library?

2 Answers 2

4

First option

You can use $templateCache service and

commonWidgets.run(function($templateCache) {
  $templateCache.put('/views/grid.html', 'This is the content of the template');
});

as long as commonWidgets is the name for module

Second option

You can use grunt-angular-templates and generate $templateCache from existing templates.

Some combination of first and second possibility will be the best for you.

ngtemplates task

Part of my Gruntfile, task ngtemplates with is called upon build process

ngtemplates: {
      app: {
        cwd: 'app',
        src: 'views/**/*.html', // all subdirectories
        dest: '.tmp/templates.js',
        options: {
          htmlmin: '<%= htmlmin.dist.options %>',
          usemin: 'scripts/modules.js', // append .tmp/tempaltes to scripts/modules.js file
          module: appName // global appName from Gruntfile config section
        }
      }
    }
Sign up to request clarification or add additional context in comments.

Comments

1

The best way is to use Grunt to build your HTML into a single JS file.

Grunt has a plugin, grunt-angular-templates that converts all your HTML templates into a JavaScript file and registers them in Angular, so that you don't have to change any code.

Then, you can simply distribute your generated JS instead of your HTML files.

Comments

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.