首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ng-html2js进行templateUrl测试的指令

使用ng-html2js进行templateUrl测试的指令
EN

Stack Overflow用户
提问于 2014-04-09 15:06:45
回答 1查看 3.5K关注 0票数 5

我用内联模板创建了一个指令,并对其进行了测试,一切都很好。现在,我已经将模板放入分离的.html文件中,并通过templateUrl指令引用它。指令在我的页面上有效,但是测试失败了,上面写着:

代码语言:javascript
复制
        Error: [$injector:modulerr] Failed to instantiate module source/html/par
tials/template-directive-my-directive.html due to:
        Error: [$injector:nomod] Module 'source/html/partials/template-directive
-my-directive.html' is not available! You either misspelled the module name
or forgot to load it. If registering a module ensure that you specify the depend
encies as the second argument.

我使用没有运气的ng-html2js预处理器 (上面的错误),在业力配置和单元测试模块加载中尝试不同的路径和前缀。有人能看到什么是错的吗(哪条路,或者什么不同的东西?)

我的结构是:

代码语言:javascript
复制
ui/Gruntfile.js // contains the karma config
ui/source/html/index.html
ui/source/html/partials/template-directive-my-directive.html
ui/source/js/my-directive.js
ui/source/tests/unit/unit-my-directive.js

在Gruntfile.js内部:

代码语言:javascript
复制
karma : {
    unit : {
        options : {
            files : [
                'source/lib/angular/angular.js', // angular first
                'source/lib/angular/*.js', // then any other angular files...
                'source/lib/x2js/xml2json.min.js',
                'source/lib/jquery/jquery.js',
                'source/lib/ui-bootstrap/ui-bootstrap.js',
                'source/js/*.js',
                'source/tests/unit/*.js',
                'source/html/partials/*.html', // because of directive templates
            ],
            autoWatch : true,
            frameworks : ['jasmine', 'detectBrowsers'],
            plugins : [
                'karma-junit-reporter',
                'karma-jasmine',
                'karma-chrome-launcher',
                'karma-firefox-launcher',
                'karma-ie-launcher',
                'karma-safari-launcher',
                'karma-opera-launcher',
                'karma-phantomjs-launcher',
                'karma-detect-browsers'
            ],
            // generate js files from html templates to expose them during testing
            preprocessors : {
                'source/html/partials/*.html' : 'ng-html2js'
            }
            // ,
            // ngHtml2JsPreprocessor : {
                // stripPrefix : 'source/html/', // tried this, no luck
                // moduleName: 'foo' // tried this, no luck
            // }
        }
    }
}

在我的测试中(unit- my -directive.js):

代码语言:javascript
复制
// load the template
beforeEach(module('source/html/partials/template-directive-my-directive.html'));

在我的指令(my-directive.js)中:

代码语言:javascript
复制
templateUrl : 'partials/template-directive-my-directive.html', // this is ok, because actual app works

ui文件夹中运行测试,Gruntfile.js驻留在该文件夹中

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-11 09:09:36

解决方案:在我花了几个小时之后,这个ST回答帮助了我。我只需要在业力插件中注册ng-html2js。

代码语言:javascript
复制
plugins : [
                        'karma-junit-reporter',
                        'karma-jasmine',
                        'karma-chrome-launcher',
                        'karma-firefox-launcher',
                        'karma-ie-launcher',
                        'karma-safari-launcher',
                        'karma-opera-launcher',
                        'karma-phantomjs-launcher',
                        'karma-detect-browsers',
                        'karma-ng-html2js-preprocessor' //added this
                    ],
票数 17
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22966697

复制
相关文章

相似问题

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