简体   繁体   English

AngularJS:在编译之前获取指令的html内容

[英]AngularJS: Get html content of a directive before compile

I'm creating a directive that shows a modal when user click on a button: 我正在创建一个指令,当用户点击按钮时会显示一个模态:

The directive template: 指令模板:

<button ng-click="showModal()">Show Modal</button>

Directive usage: 指令用法:

<modal-btn>
  <div id="modal-content">
    <p>This is the modal content that I want to render in the Modal {{someModel}}</p>
  </div>
</modal-btn>

So before I compile the directive, I need to grap the content, show the button, and when the user clicks on the button I show the modal containing the <div id="modal-content">.... 所以在编译指令之前,我需要抓住内容,显示按钮,当用户点击按钮时,我会显示包含<div id="modal-content">....

How to get directive inner content before compiling it and replacing it by the template 如何在编译之前获取指令内部内容并将其替换为模板

Hm.. it's actually quite interesting pattern for using modal with local content like this. 嗯..这实际上非常有趣的模式使用模态与这样的本地内容。

So, to accomplish that all you need is transclude option of angular directive. 所以,要完成所有你需要的是transclude选项的angular指令。 There is a good article about transclude . 一篇关于transclude好文章

HTML HTML

<modal-btn>
  <div class="modal-content">
    Lorem Ipsum Dolor Sit Amet `{{currentScopeData}}`
  </div>
</modal-btn>

We create the modal btn directive and modal content inside it. 我们在其中创建模态btn指令和模态内容。 Take a look at expression - at this point we can actually use the currentScopeData from our current scope (Controller of the page or whatever). 看一下表达式 - 此时我们实际上可以使用当前作用域中的currentScopeData (页面控制器或其他)。

Directive Template 指令模板

<button ng-click="showModal()">Show Modal</button>

Just a button with ng-click like yours. 只需按一下按钮即可点击你的按钮。

Directive 指示

App.directive('modalBtn', ['Config', function (Config) {

    function link(scope, element, attrs, ctrl, transclude) {

        // Get your modal holder element which may be located anywhere in your application
        var modalHolder = angular.element(document.querySelector('#modal-holder'));

        /**
         * Get transcluded content (which is located inside <modal-btn></modal-btn>)
         *     and set it into scope
         */
        transclude(scope, function(clone, scope) {
            scope.modalContent = clone;
        });

        /**
         * On button click set our transcluded content to the modal holder area 
         */
        scope.showModal = function () {
            modalHolder.empty().append(scope.modalContent);
        }
    }

    return {
        templateUrl: Config.rootPath + 'shared/modal/modal-btn-view.html',
        link: link,
        replace: true,
        // Set transclude option
        transclude: true
    };
}]);

All actions are commented. 所有操作都被评论。 Generally we get our inner content from transcluded function provided inside directive and set it to the scope. 通常我们从指令内提供的transcluded函数中获取内部内容,并将其设置为范围。 And when user clicks on the button showModal fired and insert our content in some modal holder, which may be anywhere in your html file. 当用户点击showModal按钮时,我们将内容插入到某个模式持有者中,这可能位于您的html文件中的任何位置。

After content is inserted you need to provide some modal showing functional (maybe add class on the modalHolder or something like that it's up to you). 插入内容后,您需要提供一些显示功能的模态(可能在modalHolder上添加类或者类似于您的内容)。

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

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