【问题标题】:Don't work $compile in Jasmine Karma Angular不要在 Jasmine Karma Angular 中使用 $compile
【发布时间】:2015-08-05 08:21:37
【问题描述】:
    (function () {
    'use strict';

    angular
        .module('app.widgets')
        .directive('analiticsHere', analitics);

    function analitics() {
        var directive = {
            template: '<div>hello world</div>'
        };
        return directive;
    }
})();

创建简单指令。 Ant 尝试测试一下。

inject(function($compile, $rootScope, $templateCache){
            compile = $compile;
            scope = $rootScope.$new();
            template = $templateCache.get('scripts/widgets/templates/Analytics.html');
            console.log(template);//return nothing
            var element = angular.element('<div analitics-here></div>');
            var compiledElement = compile(element)(scope);
            console.log('compiledElement'); //return <div analitics-here="" class="ng-scope"></div>
        });

如果我在指令中使用模板,请不要添加模板代码。 但是 ngHtml2JsPreprocessor 从我的任何 html 文件中创建 html.js。

如何使它工作?有任何想法吗?

【问题讨论】:

    标签: javascript angularjs unit-testing karma-runner karma-jasmine


    【解决方案1】:

    试试下面的代码

    inject(function($injector){
            compile = $injector.get("$compile");
            scope = $injector.get("$rootScope").$new();
            template =$injector.get("$templateCache").get('scripts/widgets/templates/Analytics.html');
            console.log(template);
            var element = angular.element('<div analitics-here></div>');
            var compiledElement = compile(element)(scope);
            console.log('compiledElement'); class="ng-scope"></div>
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-07
      • 2015-02-10
      • 1970-01-01
      • 2017-05-01
      • 2015-11-12
      • 2017-06-10
      • 2015-12-03
      相关资源
      最近更新 更多