2013-10-15 30 views
0

有沒有辦法將角度「活動DOM」轉換爲扁平化的純HTML? 我的意思是把這個:如何從角度指令和幫助註釋清理html?

<!-- ngRepeat: ref in refs track by $index --> 
<p ng_repeat="ref in refs track by $index" class="ng-scope ng-binding">a0120</p> 
<p ng_repeat="ref in refs track by $index" class="ng-scope ng-binding">a0241</p> 
<p ng_repeat="ref in refs track by $index" class="ng-scope ng-binding">z1242</p> 
<p ng_repeat="ref in refs track by $index" class="ng-scope ng-binding">a0120</p> 

到這一點:

<p>a0120</p> 
<p>a0241</p> 
<p>z1242</p> 
<p>a0120</p> 

當然,類和將保持屬性沒有關係的角度編譯過程。

感謝

感謝

+1

請問您是否可以詳細說明「live DOM」的含義?一旦角度編譯DOM(在任何東西變成功能之前必須做的),你最終會得到一個基本上正常的JavaScript網頁。這是你問的嗎? – Andyrooger

回答

1

猜測最好的方法是把遍歷DOM

下面是我使用的,如果有人想的使用它,提高它或改變它完全地代碼...

的Attr和類名單顯然沒有完成。

function cleanAngularStuff(el) { //recursive function 

    var remClassList = ['ng-scope', 'ng-model', 'ng-binding', 'ng-isolate-scope'] 
    var remAttrList = ['ng-repeat'] 

    // If node is a comment just remove it 
    if (el.nodeType == 8) { 
     el.parentNode.removeChild(el); 
    } 
    // If its an element remove extra attributes and classes and recurse children 
    else if (el.nodeType == 1) { 

     // Remove extra classes. If none is left remove class attribute 
     for (var i = 0; i < remClassList.length; i++) { 
      el.classList.remove(remClassList[i]); 
      if (el.classList.length == 0) { 
       el.removeAttribute('class') 
      } 
     } 

     // Remove attributes 
     for (var h = 0; h < remAttrList.length; h++) { 
      el.removeAttribute(remAttrList[h]) 

     } 

     // Recurse children 
     for (var i = 0, len = el.childNodes.length; i < len; i++) { 
      cleanAngularStuff(el.childNodes[i]) 
      // If child comment is removed decrease cursor 
      if (len > el.childNodes.length) { 
       len = el.childNodes.length 
       i-- 
      } 
     } 

    } 
} 
+0

如果您有jQuery '$('。ng-scope,.ng-model,.ng-binding,.ng-isolate-scope')。removeClass('。ng-scope .ng-model .ng- ('ng-repeat');' – markmarijnissen

+0

@markmarijnissen在removeClass字符串中不應該有點 –