добавить директиву html из другой директивы в angularJS

0

Скриншот здесь

Когда я нажимаю кнопку, появляется модальная мода. В этом модальном я имею <template-placeholder></template-placeholder>

Когда кнопка нажата, я делаю if-check чтобы убедиться, что это правильная кнопка. Если это так, добавьте еще одну директиву вместо placeholder.

Но я не могу заставить его работать.

Вот моя директива:

.directive('modalDirective', function(){
    return {
        restrict: 'E',
        scope: {
            ctrl: '=',
            modalId: '@',
        },
        template:  ['<div id="{{modalId}}" class="modal fade" role="dialog">', 
                      '<div class="modal-dialog">',
                        '<div class="modal-content">',
                        '<div class="modal-header">',
                        '<h4 class="modal-title">Modal Header</h4>',
                        '</div>',
                        '<div class="modal-body">',
                        '<p> {{ ctrl.text }} </p>',
                        '</div>',
                        '<div class="modal-footer">',
                        '<template-placeholder></template-placeholder>',
                        '<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>',
                        '</div>',
                        '</div>', 
                        '</div>',
                        '</div>'].join(''),
        link: function(scope, element, attrs) {
            scope.$watch('modalId', function(val){
                if(val == 'protocolModal'){
                    element.find('template-placeholder').replaceWith('<test-directive></test-directive>');
                }
            });
        }
    }
})

.directive('testDirective', function(){
    return {
        restrict: 'E', 
        template: '<div>this is our other directive speaking.</div>'
    }
});
  • 1
    Почему бы вам не использовать использовать нг-если. Это упростит вещи.
  • 0
    Является ли <template-placeholder> директивой или просто фиктивным элементом?
Показать ещё 2 комментария
Теги:
angularjs-directive

3 ответа

1
Лучший ответ

Вам нужно использовать $compile:

.directive('modalDirective', ['$compile', function($compile){
    ...
    link: function(scope, element, attrs) {
        scope.$watch('modalId', function(val){
            if(val == 'protocolModal'){
                element.find('template-placeholder').replaceWith($compile('<test-directive></test-directive>')(scope));
            }
        });
    }
}])

Обновленная скрипка: https://jsfiddle.net/L0jns64m/3/

1

Зачем смотреть на modalId? Вы можете просто использовать ng-if:

'<div class="modal-footer">',
'<test-directive data-ng-if="modalId == \'protocolModal\'"></test-directive>',
'<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>',
'</div>',

Вот скрипка с ней: http://jsfiddle.net/aL0er9rv/

0

Замените шаблон-заполнитель на

<ng-if="protocolModal" test-directive></test-directive>

ng - если вы позаботитесь о компиляции и уничтожении. Это очень приятно.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню