angularjs學習系(1),angularjs學習系

來源:互聯網
上載者:User

angularjs學習系(1),angularjs學習系

講述angularjs建立指令replace,transclude域的用法


angularjs的指令建立有四種形式,比如建立的指令hello:

(1)頁面標籤的形式E:

               <hello>
  <div>angularjs建立指令</div>
  <div>replace的用法</div>
</hello> 

(2)標籤屬性的形式A(angularjs預設的):<div hello></div>

(3)標籤樣式類的形式C:<div class="hello"></div>

(4)頁面注釋的形式M: <!-- directive:hello -->(注釋號前後有個空格)


指令的建立代碼:

myApp.directive('hello',function(){return {restrict:'ACEM',template:'<div>hello everyone!</div>',//transclude:falsereplace:false}});
【一】如果replace設定為false,最終頁面展現的html結果如下:

      C: <div class="hello"><div>hello everyone!</div></div>      A: <div hello=""><div>hello everyone!</div></div>      E: <hello><div>hello everyone!</div></hello>       M: <!-- directive:hello -->(沒有效果,無效的)
【二】如果replace設定為true,最終頁面展現的html結果如下:
      C:<div class="hello">hello everyone!</div>      A:<div hello="">hello everyone!</div>      E:<div>hello everyone!</div>       M:<div hello="">hello everyone!</div> (有效)


使用replace有個什麼缺點呢,比如我要把文章開頭指令hello下的<div>angularjs建立指令</div> <div>replace的用法</div>要顯示在頁面上,replace是辦不到的,這個可以用transclude域的設定來解決:

myApp.directive('hello',function(){return {restrict:'ACEM',template:'<div>hello everyone!<span ng-transclude></span></div>',transclude:true}});
這樣就可以把指令內的DOM標籤展現出來:

       C:<div class="hello"><div>hello everyone!<span ng-transclude=""><span class="ng-scope">樣式類</span></span></div></div>       A:<div hello=""><div>hello everyone!<span ng-transclude=""><span class="ng-scope">屬性</span></span></div></div>       E:<hello><div>hello everyone!<span ng-transclude="">   <div class="ng-scope">angularjs建立指令</div>   <div class="ng-scope">replace的用法</div></span></div></hello>        M:<!-- directive:hello -->




參考:http://stackoverflow.com/questions/15285635/how-to-use-replace-of-directive-definition

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.