5

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 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

how did you remove the / from the beginning?
just removed the '/' from beginning of the templateUrl settings, ie by setting : templateUrl:'components/directives/collapse.html', without the starting /
1

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.

1 Comment

sorry couldn't get you. how to add template module as a dependency? Also the code works normally in gulp serve, but only breaks in gulp serve:dist
0

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';
  });
})

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.