【发布时间】:2013-10-22 00:13:51
【问题描述】:
我无法让 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'
...
在规范文件中:
describe('Loading directive', function() {
...
beforeEach(module('/app/directives/loading/templates/loading.html'));
...
});
我收到以下错误:
无法实例化模块 /app/directives/loading/templates/loading.html 由于: 错误:没有模块:/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>');
});
所以看来生成的js文件是正确的,但是karma没有加载...
另外,如果我使用 --log-level 调试,这里是与模板相关的行:
调试[preprocessor.html2js]:处理“/home/rightink/public_html/bo2/master/web/app/directives/loading/templates/loading.html”
DEBUG [watcher]:已解析的文件:
/correct/path/to/the/app/directives/loading/templates/loading.html.js
我错过了什么吗?
谢谢,
【问题讨论】:
-
尝试通过查看github.com/vojtajina/ng-directive-testing 存储库来找出答案。
-
我学会了用这个 repo 测试 AngularJS 指令。所以我从一开始就受到了启发。
-
找到解决方案了吗?
-
您是否尝试过在浏览器中以调试模式运行它并查看 HTML 源代码?看到脚本标签中列出的 .html.js 文件了吗?
标签: templates angularjs testing directive karma-runner