angularjs apply multiple directives same element

折月煮酒 提交于 2019-12-24 12:04:03

问题


I have a directive with templateUrl for displaying a custom made dropdown. This directive appends DOM elements to element node without replacing it:

<cross-dropdown>

    <!-- First Directive Template Content Start-->
    <div></div>
    <!-- First Directive Template Content End-->

</cross-dropdown>

Then I want to create another one for validations, so It can append more DOM elements into the content inserted by first directive, so the result will be:

<cross-dropdown>

    <!-- First Directive Template Content Start-->
    <div></div>
    <!-- First Directive Template Content End-->

    <!-- Second Directive Template Content Start-->
    <div></div>
    <!-- Second Directive Template Content End-->

</cross-dropdown>

So here we have first directive definition:

angular.module('someModule').directive('firstDirective'....
    templateUrl: '/4_Common/1_views/directives/dropdown.html',
    replace: false,
    priority:10,

And here second one definition:

angular.module('someModule').directive('secondDirective'....
    templateUrl: 'some-template',
    transclude: true,
    replace: true,
    priority: 1

Second directive template:

<div>
    <span ng-transclude></span>
    <span>
        <span></span>
    </span>
</div>

So I can move content generated by first directive to ng-transclude span to have finally this content:

<cross-dropdown>
    <div>
        <span ng-transclude>
            <!-- First Directive Template Content Start-->
                <div></div>
            <!-- First Directive Template Content End-->
        </span>

        <!-- Second Directive Template Content Start-->
            <span>
                 <span></span>
            </span>
        <!-- Second Directive Template Content End-->
    </div>
</cross-dropdown>

But I'm getting error "Multiple Directives Asking for template". After playing with priority and check that I can't fix this error, I want to ask if what I'm trying to achieve is possible anyway or breaks directives rules. Note that I have a working version using element and compile to append this DOM content instead of using template, but I would like to keep template in a separate HTML file. Thanks in advance!

来源:https://stackoverflow.com/questions/27962378/angularjs-apply-multiple-directives-same-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!