简体   繁体   English

AngularJS + Karma + Ng-html2js =>无法实例化模块... html

[英]AngularJS + Karma + Ng-html2js => Failed to instantiate module …html

I can't make Karma working for directives that have external templates. 我无法使Karma适用于具有外部模板的指令。

Here is my karma configuration file : 这是我的业力配置文件:

preprocessors: {
    'directives/loading/templates/loading.html': 'ng-html2js'
},

files: [
    ...
    'directives/loading/templates/loading.html',
]

ngHtml2JsPreprocessor: {
    prependPrefix: '/app/'
},

In the directive file : 在指令文件中:

...
templateUrl: '/app/directives/loading/templates/loading.html'
...

In the spec file : 在spec文件中:

describe('Loading directive', function() {
    ...
    beforeEach(module('/app/directives/loading/templates/loading.html'));
    ...
});

I get the following error : 我收到以下错误:

Failed to instantiate module /app/directives/loading/templates/loading.html due to: Error: No module: /app/directives/loading/templates/loading.html 由于以下原因无法实例化模块/app/directives/loading/templates/loading.html:错误:无模块:/app/directives/loading/templates/loading.html

If I modify the source code of the karma-ng-html2js-preprocessor to print the result of the generated file, I get : 如果我修改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>');
});

So it seems that the generated JS file is correct but not loaded by karma... 所以似乎生成的JS文件是正确的但没有被karma加载......

Also, if I use --log-level debug, here are the lines related to the template : 另外,如果我使用--log-level debug,这里是与模板相关的行:

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

DEBUG [watcher]: Resolved files: DEBUG [观察者]:已解决的文件:

  /correct/path/to/the/app/directives/loading/templates/loading.html.js 

Am I missing something ? 我错过了什么吗?

Thanks, 谢谢,

The problem may be that relative paths specified in file section get expanded to full ones. 问题可能是file部分中指定的相对路径扩展为完整路径。

Something like directives/loading/templates/loading.html => /home/joe/project/angular-app/directives/loading/templates/loading.html directives/loading/templates/loading.html => /home/joe/project/angular-app/directives/loading/templates/loading.html

... and then, templates get registered with theirs full paths. ...然后,模板将在其完整路径中注册。

The solution is to configure the ng-html2js preprocessor to remove the absolute part of the template paths. 解决方案是配置ng-html2js预处理器以删除模板路径的绝对部分。 For instance, in the karma.conf.js file add the stripPrefix directive like this : 例如,在karma.conf.js文件中添加stripPrefix指令,如下所示:

ngHtml2JsPreprocessor: {
    // strip this from the file path
    stripPrefix: '.*/project/angular-app/'
    prependPrefix: '/app/'
}

Note that stripPrefix is a regexp. 请注意,stripPrefix是一个正则表达式。

You can have the pre-processor cache your templates to a module, which can then be included prior to your tests: 您可以让预处理器将模板缓存到模块,然后可以在测试之前将模板包含在模块中:

karma.conf.js karma.conf.js

files: [
  ...
  'app/**/*.html'
],

preprocessors: {
  'app/**/*.html': ['ng-html2js']
},

ngHtml2JsPreprocessor: {
   moduleName: 'templates'
},

directive file 指令文件

...
templateUrl: 'app/path-to-your/template.html',
...

spec file spec文件

describe('My directive', function() {

  beforeEach(module('templates'));
  ...
});

This may not be your exact issue, but in our application we needed to add the following to karma.conf.js: 这可能不是您的确切问题,但在我们的应用程序中,我们需要将以下内容添加到karma.conf.js:

ngHtml2JsPreprocessor: {
    cacheIdFromPath: function(filepath) {
        return '/vision/assets/' + filepath;
    }
}

The corresponding preprocessors setting looks like: 相应的预处理器设置如下所示:

preprocessors: {
    'views/**/*.html': 'html2js'
},

My understanding was that this was due to using absolute URLs in AngularJS when specifying templates - which karma was rewriting when running tests? 我的理解是,这是因为在指定模板时使用AngularJS中的绝对URL - 运行测试时业力是否被重写?

Anyway hope this helps. 无论如何希望这有帮助。

I'm in the process of learning AngularJS and ran into the same problem. 我正在学习AngularJS并遇到同样的问题。 I have no idea why but changing the port in karma.conf.js fixed it for me. 我不知道为什么但是改变karma.conf.js中的端口为我修复了它。

module.exports = function(config){
  config.set({

    ...

    port: 9877,

    ...

  });
};

Edit: 编辑:

After a bit more testing I found that the problem was only happening on Chrome, and was resolved by explicitly clearing all of the browser history (Ctrl + F5 didn't work). 经过一些测试后,我发现问题只发生在Chrome上,并通过明确清除所有浏览器历史记录(Ctrl + F5无法正常工作)解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM