5
votes

My build.js is using ngHtml2js to convert partials in directives into js files.

gulp.task('partials', function () {
  return gulp.src('src/{components,app}/**/*.html')
    .pipe($.ngHtml2js({
      moduleName: 'testApp'
    }))
    .pipe(gulp.dest('.tmp'))
    .pipe($.size());
});

Where, my directives are situated in components folder in components->directives.

The issue is that when I try to deploy the dist folder, the static partial calls from the routes JS files gets fired, and I get a 404.

angular.module(ModuleName)
        .directive('collapseWindow', ['$parse', function (parse) {

            return {
                templateUrl:'/components/directives/collapse.html',
                transclude:true,
                restrict: 'A'

In my understanding, the ngHtml2js converts the partials into module.run blocks as

module.run(['$templateCache', function($templateCache) {
  $templateCache.put('components/directives/collapse.html',
    '<div class="collapsible">\n' ...

But then, why am I getting a 404 error such as

 GET http://localhost:3000/components/directives/collapse.html 404 (Not Found)
3

3 Answers

4
votes

Solved it. The reason was templateUrl were given absolute paths (with '/' at beginning), but templateCache uses relative paths. Thus removing the '/' from templateUrls solved the issue

1
votes

You need to add the template module as a dependency in your Angular application module. Also ensure that your template cache js file has been loaded in browser before your application code.

0
votes

I added this config for main app. It overwrites template mapping for ngnewroute

.config (function ($componentLoaderProvider) {
  $componentLoaderProvider.setTemplateMapping(function (name) {
    return 'components/' + name + '/' + name + '.html';
  });
})