Не работает $compile в Jasmine Karma Angular

    (function () {
    'use strict';

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

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

Создайте простую директиву. Муравей попробуй проверить.

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.js из любых моих html-файлов.

Как заставить это работать? Любые идеи?


person Николаев Сергей    schedule 05.08.2015    source источник


Ответы (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>
    });
person samyak bhalerao    schedule 25.09.2015