加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

模板 – AngularJS Karma Ng-html2js =>无法实例化模块… htm

发布时间:2020-12-17 08:29:44 所属栏目:安全 来源:网络整理
导读:我不能使Karma工作的指令有外部模板。 这里是我的karma配置文件: preprocessors: { 'directives/loading/templates/loading.html': 'ng-html2js'},files: [ ... 'directives/loading/templates/loading.html',]ngHtml2JsPreprocessor: { prependPrefix: '/a
我不能使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>');
});

所以看来生成的JS文件是正确的,但不是由karma加载…

此外,如果我使用–log级调试,这里是与模板相关的行:

DEBUG [preprocessor.html2js]: Processing “/home/rightink/public_html/bo2/master/web/app/directives/loading/templates/loading.html”

DEBUG [watcher]: Resolved files:

06004

我缺少什么?

谢谢,

问题可能是在文件节中指定的相对路径被扩展为完整的。

类似于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。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读