简体   繁体   English

如何在另一个指令中呈现Angular指令?

[英]How can you render an Angular directive inside another directive?

Is there a way that I can create a child directive inside of a parent directive and use the following modal attribute (in the child directive) which would insert HTML from a template into a Bootstrap modal? 有没有一种方法可以在父指令内部创建子指令,并使用以下模式属性(在子指令中),该属性会将模板中的HTML插入Bootstrap模态? The child directive would include a question attribute and a modal attribute with the template. 子指令将在模板中包含一个问题属性和一个模态属性。

This article does not help because, in this instance, I need specific child directives with unique attributes, namely the question and modal attribute. 本文无济于事,因为在这种情况下,我需要具有唯一属性(即问题和模式属性)的特定子指令。

HTML: HTML:

<learn-card title="Eligibility">
    <learn-item question="Who Can Purchase a meal plan" modal="/elegibility/HowDoISignup.html"></learn-item>
    <learn-item question="How Do I sign up for a meal plan?" modal=""></learn-item>
</learn-card>

Directive: 指示:

In the below example I assume I will need to add the bootstrap 4 modal inside of my template code and allow the content to change when a question is clicked. 在下面的示例中,我假设我将需要在模板代码中添加bootstrap 4模态,并允许在单击问题时更改内容。 Would this be easier than having all modals added to the page before hand? 这比将所有模态预先添加到页面容易吗? Do I need to somehow compile the HTML from this directive and concatenate the HTML from the other directive? 我是否需要以某种方式编译此指令中的HTML并连接其他指令中的HTML?

angular.module('main').directive('learnCard', function($http, $compile){
return{ 

    compile: function(element, attrs){ 
        console.log(attrs);
    },

    template: "<div class='col-sm-4'>"+
    "<div class='learn-card box-shadow'>" + 
        "<h3 class='text-center'>{{title}}</h2>" +
        "<ul></ul>" +
    "</div>" +
    "</div>",
    scope: {
        title: '@title'
    },
    link: function( scope, element, attrs ){
            console.log(attrs);
            element.compile.html(element);
            element.bind(attrs.title, function(){
            }); 
    }
}
});

Here I have all of the individual questions inside of the parent directive. 在这里,我在parent指令中包含了所有单个问题。 Running this independently does not render any questions. 独立运行此命令不会引起任何问题。

angular.module('main').directive('learnItem', function(){
return{ 
    template: "<li data-toggle='modal' data-target='{{modalId}}'">{{question}}</li>", 
    scope: {
        question: '@question'
    },
    link: function( scope, element, attrs ){
        alert('working');
        console.log($('learn-item'));
    }

}
});

在此处输入图片说明

The first problem is having Angular transclude into the parent tag (learn-card). 第一个问题是将Angular包含到父标记(学习卡)中。 This was as easy as setting transclude:true in the directive. 这就像在指令中设置transclude:true一样容易。 Adding this property allowed me not to assign the ng-transclude attribute to parent nodes. 添加此属性使我无法将ng-transclude属性分配给父节点。

In my other directive (learn-item) I was then able to solve my second issue: setting the contents of a file into a Bootstrap modal when assigning a value to the unique attribute "modal" 然后,在另一个指令(学习项目)中,我可以解决第二个问题:将值分配给唯一属性“ modal”时,将文件内容设置为Bootstrap modal

The workaround to this problem was to assign a controller to the child directive (learn-item) which includes an Angular timeout function. 解决此问题的方法是将一个控制器分配给包含Angular超时功能的子指令(学习项目)。 Inside of this function, I assigned the return of the HTTP method. 在此函数内部,我分配了HTTP方法的返回值。 Unfortunately, using the compile function or link function would not work because scope would be set on the last item meaning that every model would include the same body text. 不幸的是,使用编译功能或链接功能将不起作用,因为范围将设置在最后一项上,这意味着每个模型都将包含相同的主体文本。 I assume this is because of the inheritance of scope . 我认为这是因为scope的继承。 Using $timeout added the scope to the current object instead of every expression. 使用$timeout将范围添加到当前对象,而不是每个表达式。

return{ 
    template:'<li><a href="" data-toggle="modal" data-target="#{{id}}">    {{question}}</a></li>' + modal, 
    scope: {
            question: '@question',
            id: '@dataid',
            modal: '@modal',
    },
    controller: ['$scope', function($scope){
        $timeout(function(){
            var modal = $scope.modal; 
            if ( modal !== undefined){
                $http.get('/pages/faq/'+modal).then(function(response){
                    $scope.data = response.data;                     
                });
            }

        });

    }]

} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM