javascript - 如何正确使用 karma-ng-html2js-preprocessor?

标签 javascript angularjs unit-testing templates

我有指令:

angular
    .module('app')
    .directive('stat24hour', stat24hour);

function stat24hour(req) {
    var directive = {
        link: link,
        template: 'scripts/widgets/templates/stat24Hour.html'
    };
    return directive;

    function link(scope) {
        req.option('stats').then(function(data){
            scope.stats = data;
        });
    }
}

我配置 karma 使我的模板 *.html 像模块

(function(module) {
try {
  module = angular.module('template');
} catch (e) {
  module = angular.module('template', []);
}
module.run(function($templateCache) {
  $templateCache.put('scripts/widgets/templates/stat24Hour.html','Here HTML');
});
})();

但是当我尝试使用时

    angular.module('template');
inject(function($compile, $rootScope){
    compile = $compile;
    scope = $rootScope.$new();
    var element = angular.element('div stat24hour');
    var compiledElement = compile(element)(scope);
    console.log(compiledElement);
});

编译返回 html,不使用指令模板。 需要修复什么才能使其正常工作?

最佳答案

您的指令中有 template: '..',加载外部资源时应使用 templateUrl。 template:表示静态字符串作为模板

关于javascript - 如何正确使用 karma-ng-html2js-preprocessor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31816021/

相关文章:

javascript - 查找目录的内容

javascript - 不要在 Javascript 函数上重复自己

javascript - 动态 <svg-icon> 作为 Angular 指令

angularjs - 使用没有值的 ng-app

scala - 在所有测试运行之前仅初始化一次值

javascript - jQuery:setInterval 对服务器的影响?

python - Flask/AngularJS 部署到 Heroku

ruby - 突变测试 : does anybody know mutant?

c# - C# 单元测试中未启用 ILogger.LogLevel

javascript - Mac OS - 使用 Node.js 锁定文件