javascript - AngularJS Directive Template URL Requires Leading Slash, Breaks Tests -
'use strict'; directives.directive('primaryclient', function() { return { restrict: 'a', templateurl: 'views/directives/primary-client.html', scope: { 'client': '=' } }; });
i've got simple directive replaces element contents of template file. shown above, it's broken. error in chrome error: unexpected request: views/directives/primary-client.html
. adding leading slash (i.e. /views/directives/primary-client.html
) fixes problem.
however
in tests, absolutely unable working leading slash. file loaded , passing test when omit leading slash, of course breaks actual functionality.
i can share more code tests if needed, examples of tests loaded templates seem show directive omitting leading slash. i've got <base href="/">
set in index.html. there i'm doing wrong that's forcing leading slash?
i ran issue relative paths , tests. in directives have leading slash:
templateurl: '/dist/js/templates/my-template.html'
this works fine in app, when run tests unexpected request: get
error.
i able fix using karma-ng-html2js-preprocessor , playing around relative versus non-relative filepaths until working.
basically, in karma config, preprocessors have (notice no slash before public):
preprocessors: { 'public/js/templates/**/*.html': ['ng-html2js'] },
in file list in karma config have (again, no slash before public):
files: [ 'public/js/templates/**/*.html' ],
then set preprocessor in karma config follows (notice slash before dist, that's important part)
nghtml2jspreprocessor: { stripprefix: 'public', prependprefix: '/dist', modulename: 'dir-templates' }
this means preprocessor take template file @ public/js/templates/my-template.html
, put in cache under /dist/js/templates/my-template.html
then in actual test import special module dir-templates
, defined under modulename preprocessor above, so:
beforeeach( module('app') ); beforeeach( module('dir-templates') );
and works!
Comments
Post a Comment