2013-10-15 99 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字符串中不应该有点 –