簡體   English   中英

AngularJS:在編譯之前獲取指令內容

[英]AngularJS: Get directive content before compiling

我想在編譯之前獲取指令的html內容。

這是一個例子:

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

我想獲取my-directive所有內容並將其從其中刪除,並在其他地方使用它( 不在self指令內部

因此,換句話說,我想訪問指令DOM,進行一些更改,然后對其進行編譯。

如果要在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');
            }
        }
    };
});

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM