S
S
Stepan2015-02-09 16:45:16
JavaScript
Stepan, 2015-02-09 16:45:16

Why isn't the template rendered when using $compile in a directive?

directive('addSubtitle', function($compile) {

    var subtitleTemplate =
        '<div class="entry-photo">' +
            '<h2>&nbsp;</h2>' +
            '<div class="entry-img">' +
                '<span>' +
                    '<a href="">' +
                        '<img ng-src="" alt="entry photo">' +
                    '</a>' +
                '</span>' +
            '</div>' +
        '' +
            '<div class="entry-text">' +
                '<div class="entry-title"></div>' +
                '<div class="entry-copy"></div>' +
            '</div>' +
        '</div>';

    var getTemplate = function(contentType) {
        var template = '';

        switch(contentType) {
            case 'subtitle':
                template = subtitleTemplate;
                break;
            case 'video':
                template = videoTemplate;
                break;
            case 'notes':
                template = noteTemplate;
                break;
        }
        return template;
    };

    var linker = function(scope, element, attrs) {

        element.click(function(){
            console.log(attrs.contentType);
            console.log(scope.type);

            $compile(getTemplate(scope.type.type));
        });
    };

    return {
        restrict: "EA",
        scope: {
            type: '=contentType'
        },
        replace: false,
        link: linker
    };

});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Dedukhin, 2015-02-10
@Demetros

Read the documentation for the $compile service.
Your call returns a function that can be called with the scope parameter and get the element.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question