Не работете с $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;
    }
})();

Създайте проста директива. 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.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