简体   繁体   English

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

[英]AngularJS: Get directive content before compiling

I want to get the html content of a directive before it gets compiled. 我想在编译之前获取指令的html内容。

Here is an example: 这是一个例子:

<my-directive>
  <ul>
    <li ng-repeat="item in items">{{item.label}}</li>
  </ul>
</my-directive>

I want to get all the content of my-directive and remove it from it and use it in other place ( Not inside the directive it self ) 我想获取my-directive所有内容并将其从其中删除,并在其他地方使用它( 不在self指令内部

So in other words I want to get access to the directive DOM, do some changes, and then compile it. 因此,换句话说,我想访问指令DOM,进行一些更改,然后对其进行编译。

If you want to grab directive content before it gets compiled by Angular, then you need to use compile function of the directive: 如果要在Angular编译指令内容之前获取指令内容,则需要使用指令的编译功能:

app.directive('myDirective', function() {
    return {
        compile: function(tElement) {

            var html = tElement.html();
            console.log(html);

            // return link function if needed
            return function(scope, element) {
                console.log('link function');
            }
        }
    };
});

Demo: http://plnkr.co/edit/E5uuZY74iYc3g9s6sZkc?p=preview 演示: http //plnkr.co/edit/E5uuZY74iYc3g9s6sZkc?p = preview

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

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