使用AngularJS从文本中删除HTML

时间:2014-08-07 14:28:04

标签: angularjs

以下是我要清理的文字:

Infographic: What is BPA? Why does it get such a bad rap? Discover more about it in our       
infographic - Why BPA will not kill you. (See a larger version here: <a   
rel="nofollow">https://www.containerandpackaging.com/sam/blog/infographics/infographic_ 
WhyBPAWontKillYou.jpg</a> )<br/><br/><a rel="nofollow" target="_blank" 
href="http://www.facebook.com/containerandpackaging/photos/a.268688407811.176198.266913
322811/10152627992307812/?type=1&relevant_count=1" id="" title="" style=""><img   
class="img" src="http://sphotos-e.ak.fbcdn.net/hphotos-ak-xpa1/v/t1.0-  
9/s130x130/10600407_10152627992307812_1312617274906388391_n.jpg?    
oh=505a27309629d804d894521dc035bce2&oe=5459BBC9&__gda__=1413295158_68fb27fae5cc75b8264a
8f5912613fc5" alt=""/></a><br/>

这是我原来的javascript代码: var content = item.description.replace(/] *&gt; / g,“”)。replace(/ \ / g,“”); content = stripHtml(content);

输出看起来像这样:

信息图:什么是BPA?为什么会这么糟糕的说唱?在我们的信息图中了解更多信息 - 为什么BPA不会杀了你。 (在这里查看更大的版本:ht ......

有没有办法以棱角分明的方式做到这一点?

3 个答案:

答案 0 :(得分:0)

AngularJS使用JavaScript作为基础,它没有任何特殊的去除HTML,所以你应该继续使用你的javascript。

答案 1 :(得分:0)

由于Angular包含jQuery,因此很容易。将HTML放在可以定位的容器中,并使用jQuery方法.text()。这是代码:

<html>
<head>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
$(document).ready(function() {
  $("#new").val($("#original").text());
})
</head>
<body>
<div id="original">
  Infographic: What is BPA? Why does it get such a bad rap? Discover more about it in our       
infographic - Why BPA will not kill you. (See a larger version here: <a   
rel="nofollow">https://www.containerandpackaging.com/sam/blog/infographics/infographic_ 
WhyBPAWontKillYou.jpg</a> )<br/><br/><a rel="nofollow" target="_blank" 
href="http://www.facebook.com/containerandpackaging/photos/a.268688407811.176198.266913
322811/10152627992307812/?type=1&relevant_count=1" id="" title="" style=""><img   
class="img" src="http://sphotos-e.ak.fbcdn.net/hphotos-ak-xpa1/v/t1.0-  
9/s130x130/10600407_10152627992307812_1312617274906388391_n.jpg?    
oh=505a27309629d804d894521dc035bce2&oe=5459BBC9&__gda__=1413295158_68fb27fae5cc75b8264a
8f5912613fc5" alt=""/></a><br/>
  </div>
  <textarea id="new" cols="40" rows="10">
  </textarea>
</body>
</html>

以下是一个有效的例子:http://jsbin.com/xovijoko/1/edit?html,js,output

答案 2 :(得分:0)