我不能使Karma工作的指令有外部模板。
这里是我的karma配置文件:
preprocessors: { 'directives/loading/templates/loading.html': 'ng-html2js' },files: [ ... 'directives/loading/templates/loading.html',] ngHtml2JsPreprocessor: { prependPrefix: '/app/' },
在指令文件中:
... templateUrl: '/app/directives/loading/templates/loading.html' ...
在spec文件中:
describe('Loading directive',function() { ... beforeEach(module('/app/directives/loading/templates/loading.html')); ... });
我得到以下错误:
Failed to instantiate module /app/directives/loading/templates/loading.html due to:
Error: No module: /app/directives/loading/templates/loading.html
如果我修改karma-ng-html2js-preprocessor的源代码来打印结果
生成文件,我得到:
angular.module('/app/directives/loading/templates/loading.html',[]).run(function($templateCache) { $templateCache.put('/app/directives/loading/templates/loading.html','<div ng-hide="hideLoading" class="loading_panel">\n' + ' <div class="center">\n' + ' <div class="content">\n' + ' <span ng-transclude></span>\n' + ' <canvas width="32" height="32"></canvas>\n' + ' </div>\n' + ' </div>\n' + '</div>'); });
此外,如果我使用–log级调试,这里是与模板相关的行:
DEBUG [preprocessor.html2js]: Processing “/home/rightink/public_html/bo2/master/web/app/directives/loading/templates/loading.html”
DEBUG [watcher]: Resolved files:
06004
我缺少什么?
谢谢,
问题可能是在文件节中指定的相对路径被扩展为完整的。
原文链接:https://www.f2er.com/angularjs/145309.html类似于directives / loading / templates / loading.html => /home/joe/project/angular-app/directives/loading/templates/loading.html
…然后,模板注册他们的完整路径。
解决方案是配置ng-html2js预处理器以删除模板路径的绝对部分。例如,在karma.conf.js文件中添加stripPrefix指令,如下所示:
ngHtml2JsPreprocessor: { // strip this from the file path stripPrefix: '.*/project/angular-app/' prependPrefix: '/app/' }
注意,stripPrefix是一个regexp。