首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AngularJS + Karma + Ng-html2js =>未能实例化模块...html

AngularJS + Karma + Ng-html2js =>未能实例化模块...html
EN

Stack Overflow用户
提问于 2013-10-14 12:17:42
回答 4查看 13.2K关注 0票数 26

我不能让Karma为有外部模板的指令工作。

这是我的业力配置文件:

代码语言:javascript
复制
preprocessors: {
    'directives/loading/templates/loading.html': 'ng-html2js'
},

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

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

在指令文件中:

代码语言:javascript
复制
...
templateUrl: '/app/directives/loading/templates/loading.html'
...

在规范文件中:

代码语言:javascript
复制
describe('Loading directive', function() {
    ...
    beforeEach(module('/app/directives/loading/templates/loading.html'));
    ...
});

我得到以下错误:

未能实例化模块/app/directives/loading/templates/loading.html,原因是: Error: No模块: /app/directives/loading/templates/loading.html

如果我修改业力-ng-html2js-预处理程序的源代码以打印生成文件的结果,我将得到:

代码语言:javascript
复制
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文件是正确的,但没有加载业力。

另外,如果我使用-日志级别的调试,下面是与模板相关的行:

调试"/home/rightink/public_html/bo2/master/web/app/directives/loading/templates/loading.html“:处理preprocessor.html2js 调试监视程序:解析文件: /correct/path/to/the/app/directives/loading/templates/loading.html.js

我漏掉了什么吗?

谢谢,

EN

回答 4

Stack Overflow用户

发布于 2014-01-31 12:11:37

问题可能是file部分中指定的相对路径被扩展到完整路径。

类似于directives/loading/templates/loading.html => /home/joe/project/angular-app/directives/loading/templates/loading.html

..。然后,模板被注册到他们的全部路径。

解决方案是配置ng-html2js预处理程序,以删除模板路径的绝对部分。例如,在karma.conf.js文件中添加如下stripPrefix指令:

代码语言:javascript
复制
ngHtml2JsPreprocessor: {
    // strip this from the file path
    stripPrefix: '.*/project/angular-app/'
    prependPrefix: '/app/'
}

注意,stripPrefix是一个regexp。

票数 12
EN

Stack Overflow用户

发布于 2014-01-03 14:25:31

您可以让预处理程序将模板缓存到模块,然后在测试之前将模板包括在内:

karma.conf.js

代码语言:javascript
复制
files: [
  ...
  'app/**/*.html'
],

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

ngHtml2JsPreprocessor: {
   moduleName: 'templates'
},

指令文件

代码语言:javascript
复制
...
templateUrl: 'app/path-to-your/template.html',
...

规范文件

代码语言:javascript
复制
describe('My directive', function() {

  beforeEach(module('templates'));
  ...
});
票数 11
EN

Stack Overflow用户

发布于 2013-10-31 03:33:03

这可能不是您的确切问题,但在我们的应用程序中,我们需要向karma.conf.js添加以下内容:

代码语言:javascript
复制
ngHtml2JsPreprocessor: {
    cacheIdFromPath: function(filepath) {
        return '/vision/assets/' + filepath;
    }
}

相应的预处理器设置如下所示:

代码语言:javascript
复制
preprocessors: {
    'views/**/*.html': 'html2js'
},

我的理解是,这是由于在AngularJS中指定模板时使用了绝对URLs -在运行测试时重写了哪些业力?

希望这能帮上忙。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19360083

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档