Angularjs 如何使用html2js预处理器?

Angularjs 如何使用html2js预处理器?,angularjs,karma-runner,Angularjs,Karma Runner,我试图通过遵循Vojtajina的指南来使用html2js预处理器,但我似乎不明白它是如何工作的 在我的例子中,我有一个指令,它使用templateUrl为指令中的html提供服务。在我的测试中,我从文件中硬编码html,但是如果html发生变化,这将成为问题 beforeEach( inject(function($rootScope,$compile) { elm = angular.element('<div class="header_wrapper shadow-ver

我试图通过遵循Vojtajina的指南来使用html2js预处理器,但我似乎不明白它是如何工作的

在我的例子中,我有一个指令,它使用
templateUrl
为指令中的html提供服务。在我的测试中,我从文件中硬编码html,但是如果html发生变化,这将成为问题

beforeEach( inject(function($rootScope,$compile) {

    elm = angular.element('<div class="header_wrapper shadow-vertical" ng-show="header.show">'+
                '<div class="header-container">'+

                    '<div class="pull-left">'+
                        '<img width="105" height="75" title="" alt=http://www.cvs.com ng-src="resources/images/logo.png">'+
                    '</div>'+

                    '<div class="pull-left" style="margin: 20px"><span class="header_title">{{header.headerTitle}}</span></div>'+

                    '<div class="logout">'+
                        '<a ng-href="#/logout" ng-click="placeHolder">Logout</a>'+
                    '</div>'+  

                    '<div class="help">'+
                        '<a ng-href="#" ng-click="placeHolder">Help</a>'+
                    '</div>'+

                    '<div class="user">'+
                        '<div>'+
                            '<b class="header-text">Corporation</b>'+
                        '</div>'+
                        '<div>Welcome {{header.headerFirstName}} {{header.headerSurname}}</div>'+
                    '</div>'+

                '</div>'+
            '</div>');
     // Compiles the directive and links it to the scope
     $compile(elm)(scope);
     scope.$digest();
}));
预处理器目标文件也在“文件”属性中设置。我没有遇到加载模块失败错误,但这可能是因为我没有以理想的方式加载html。 如何在每个之前的
块中执行此操作

我想我可以做到这一点:

elm = angular.element( '<div header-bar></div>' );
elm=angular.element(“”);
但这会导致所有测试失败,并返回一个
意外请求:GET


谢谢

您的业力配置应该如下所示:

preprocessors: {
      'src/main/webapp/partials/common/components/header-bar/header-bar.html': 'ng-html2js'
},

ngHtml2JsPreprocessor: {

  // setting this option will create only a single module that contains templates
  // from all the files, so you can load them all with module('foo')
  moduleName: 'loadTemplates'
},
beforeEach(module('loadTemplates'));
文件:[
...,
//确保模板文件包含在以下文件中:
'src/main/webapp/partials/common/components/header bar/header bar.html'
],
预处理器:{
“src/main/webapp/partials/common/components/header bar/header bar.html”:“ng-html2js”
},
NGHTML2JS预处理器:{
//使模板id与模板URL中的id匹配
stripPrefix:'src/main/webapp/',
//设置此选项将仅创建包含模板的单个模块
//从所有文件中,因此您可以使用模块('foo')加载所有文件
moduleName:'loadTemplates'
}
在单元测试中,您必须加载上面
ngHtml2JsPreprocessor
config的
moduleName:
中指定的模块,如下所示:

preprocessors: {
      'src/main/webapp/partials/common/components/header-bar/header-bar.html': 'ng-html2js'
},

ngHtml2JsPreprocessor: {

  // setting this option will create only a single module that contains templates
  // from all the files, so you can load them all with module('foo')
  moduleName: 'loadTemplates'
},
beforeEach(module('loadTemplates'));

希望这有帮助。

请显示您用作
模板url的url
。我用于此操作的url如下所示:模板url:“partials/common/components/header bar/header bar.html”感谢您的回复-这似乎起到了作用。需要注意的一点是,我认为我能做的实际上是有效的。谢谢