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

angularjs – 使用templateUrl指令 – 使用ng-html2js进行测试

发布时间:2020-12-17 06:57:13 所属栏目:安全 来源:网络整理
导读:我用内联模板创建了一个指令,对其进行了测试,一切正常.现在我将模板放入单独的.html文件中,并通过指令中的templateUrl引用它.指令适用于我的页面,但测试被打破说: Error: [$injector:modulerr] Failed to instantiate module source/html/partials/template
我用内联模板创建了一个指令,对其进行了测试,一切正常.现在我将模板放入单独的.html文件中,并通过指令中的templateUrl引用它.指令适用于我的页面,但测试被打破说:

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 preprocessor没有运气(上面的错误),在我的karma配置和单元测试模块加载中尝试不同的路径和前缀.任何人都可以看到什么是错的(哪条路径,或者可能是不同的东西?)?

我的结构是:

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里面:

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):

// load the template
beforeEach(module('source/html/partials/template-directive-my-directive.html'));

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

templateUrl : 'partials/template-directive-my-directive.html',// this is ok,because actual app works

从uru文件夹中运行测试,Gruntfile.js所在的位置

解决方法

解:
在我花了几个小时之后,这个 ST answer对我有所帮助.我只需要在业力插件中注册ng-html2js

plugins : [
                        'karma-junit-reporter','karma-detect-browsers','karma-ng-html2js-preprocessor' //added this
                    ],

(编辑:李大同)

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

    推荐文章
      热点阅读