teux / ng-cache-loader

Webpack loader to put HTML partials in the Angular's $templateCache.
83 stars 15 forks source link

module is not available when using custom module name #13

Closed santo74 closed 8 years ago

santo74 commented 8 years ago

Hi,

When I specify a custom module name like this:

    module: {
        loaders: [
            { test: /\.html$/, loader: "ng-cache-loader?prefix=/templates&module=myTemplates"}
        ]
    },

I get the following error when I load the application in the browser: "Uncaught Error: [$injector:nomod] Module '["myTemplates"]' is not available! When I omit the custom module parameter, everything works fine.

I'm new to Webpack, so I might be missing something obvious, but looking at the generated code, there seems to be missing some code to create the custom module.

This is the generated code, which results in the above error:

    var v1="<div ui-view></div>";
    window.angular.module(["myTemplates"]).run(["$templateCache",function(c){c.put("/templates/mytemplate.tpl.html", v1)}]);
    module.exports=v1;

If I add one extra line, it works fine:

    var v1="<div ui-view></div>";
    window.angular.module("myTemplates", []);
    window.angular.module(["myTemplates"]).run(["$templateCache",function(c){c.put("/templates/mytemplate.tpl.html", v1)}]);
    module.exports=v1;

Browserify, for example, does something similar. It generates the following code for each template:

var ngModule;
try {
  ngModule = angular.module('myTemplates');
} catch (e) {
  ngModule = angular.module('myTemplates', []);
}
ngModule.run(['$templateCache', function ($templateCache) {
  $templateCache.put('/templates/mytemplate.tpl.html',
    '<div ui-view></div>');
}]);

Is this a bug?

teux commented 8 years ago

Hi! It's not a bug. It means that you create the custom module outside loader. But for convenience I will add this code.

santo74 commented 8 years ago

Thanks, that would be awesome!

I'm not sure I understand your explanation though. What do you mean by "you create the custom module outside the loader"? I'm not creating any custom module myself. I just have a simple angular app with some external templates which I want to bundle with webpack, using ng-cache-loader for putting the templates in the angular cache. To ng-cache-loader I'm telling that it should use a custom module name for my templates, so I would expect that ng-cache-loader also creates that custom module for me (cfr browserify).

Again, I'm new to webpack, so I might overlook something obvious.

Thanks

teux commented 8 years ago

Now the specified module will be created. Then you can point it in the dependency:

require('ng-cache?module=myTemplates!./tpls/pagination.html');

angular.module('todoApp', ['myTemplates'])
santo74 commented 8 years ago

I just updated to ng-cache-loader v0.0.14 and now it works indeed. Thanks!