首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试角指令时,即使使用ng-html2js,也会产生“意外请求”。

测试角指令时,即使使用ng-html2js,也会产生“意外请求”。
EN

Stack Overflow用户
提问于 2014-04-04 17:49:31
回答 2查看 11.1K关注 0票数 25

在花了最后一天努力完成这项工作之后,我发现我已经回到了我刚开始时遇到的错误:

错误:意外请求:gettest-directive.html

我用卡玛和茉莉花在角上测试指令。我在StackOverflow上看过类似的问题,但发现在其他示例中尝试过的所有问题都是徒劳的。

码结构

测试-应用程序

-src

--保龄球

-解放组织

--js

-单元

-testDir

-test.js

-测试

-test.spec.js

-test

-配置

-Karma.conf.

-e2e

因果报应

代码语言:javascript
复制
'use strict';
module.exports = function(config){
    config.set({
    basePath: '../../',
    frameworks: ['jasmine'],
    files: [
        // Angular
        'src/bower/angular/angular.js',
        // Mocks
        'src/bower/angular-mocks/angular-mocks.js',
        // Libraries
        'src/lib/**/*.js',
        // App
        'src/js/*.js',
        'src/modules/*/*.js',
        // Tests
        'src/modules/**/test/*spec.js',
        // Templates
        'src/modules/**/*.html'
    ],
    autoWatch: false,
    singleRun: true,
    reporters: ['progress'],
    browsers: ['PhantomJS'],

    preprocessors: {
        'src/modules/**/*.html': 'ng-html2js'
    },
    ngHtml2JsPreprocessor: {
        moduleName: 'dir-templates'
    },
    plugins: [
        'karma-jasmine',
        'karma-ng-html2js-preprocessor',
        'karma-phantomjs-launcher',
        'karma-chrome-launcher',
        'karma-junit-reporter'
    ]
    });
};

test.js

代码语言:javascript
复制
'use strict';
angular.module('modules.test', []).
directive('testDirective', [function() {
    return {
        restrict: 'E',
        templateUrl: 'test-directive.html',
        link: function($scope, $elem, $attrs) {
            $scope.someFn = function() {
                angular.noop();
            };
        }
    };
}]);

test-direct.html

代码语言:javascript
复制
<span>Hello World</span>

test.spec.js

代码语言:javascript
复制
'use strict';
describe('test module', function() {
    beforeEach(module('modules.test'));
    /* -- DIRECTIVES------------------ */
    describe('directives', function() {
        var $compile, $scope, elm;
        beforeEach(module('dir-templates');
        beforeEach(inject(function($compile, $rootScope) {
            $scope = $rootScope.$new();
            elm = angular.element('<test-directive></test-directive>');
            $compile(elm)($scope);
            $scope.$digest();
        }));
        it('should have one span tag', function(){
            //Jasmine test here to check for one span tag.
        });
    });
});

已经缩短了几个文件,以坚持问题所在。在调用beforeEach(module('dir-templates'))时,它应该将所有匹配的.html文件加载到$templateCache中,并防止引发错误的GET请求。

任何帮助都会很感激,因为它真的让我发疯了。如果您有任何其他问题,请评论。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-04 21:05:20

所以,一个痛苦的头痛似乎是两条线的修复。打开Chrome中的Karma (而不是PhantomJS)并查看源文件之后,我注意到当ng-html2js将指令附加到$templateCache时,它使用的是整个路径,而不是指令定义中提供的路径。

简而言之,'src/modules/test/test-directive.html.js' !== 'test-directive.html.js'

为此,请将karma.conf.js文件ngHtml2JsProcessor修改为如下所示:

代码语言:javascript
复制
ngHtml2JsPreprocessor: {
    stripPrefix: 'src/',
    moduleName: 'dir-templates'
},

指令声明的templateUrl如下所示:

代码语言:javascript
复制
templateUrl: 'modules/test/test-directive.html'
票数 29
EN

Stack Overflow用户

发布于 2015-09-14 22:23:55

要添加关于确保与前缀匹配的模板名称(没有前导斜杠等)的注释,另一件要检查的是大小写。

模板缓存键区分大小写,因此请确保指令使用正确的大小写引用html文件。ngHtml2JsPreprocessor生成的模板缓存键使用文件系统上实际文件名和目录名的大小写。

因此,如果您的文件名为Test-Directive.html,或者您的文件夹名为"Modules“,但是您的指令引用的是"modules/test-directive.html",那么它将不会从缓存中解析。

实际(非测试)使用指令的templateurl ( GET请求显然不区分大小写,模板缓存键将根据初始GET请求是什么,即。无论您的指令中指定了什么)。

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

https://stackoverflow.com/questions/22869668

复制
相关文章

相似问题

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