2016-01-20 51 views
-1

我使用html制作了表单,并使用angularjs将数据发布到后端。我想在发布到后端之前转义html标签。我该怎么做呢?从用户填写的表格清理或转义html标签

如果示例用户帖子像

<p> hello world </p> 

应该被发送到服务器一样

&lt;p&gt; hello world &lt;/p&gt; 
+0

你不应该信任客户端(JS)这种类型的任务 –

回答

0

ngSanitize应该做的伎俩。请参阅link

这里有一种方法可以从客户端进行。

var app = angular.module('app',[]); app.controller( 'myCtrl',[ '$范围',函数($范围){

$scope.htmlText = "<p> Let's see</p>"; 

    $scope.htmlEncode = function(){ 
    var safeString = $('<div>').text($scope.htmlText).html(); 
    // if you don't have jquery the use the following 
    //safeString = $scope.htmlText.replace('<', '&lt;').replace('>', '&gt;');//and so on for other html special characters. 
    alert(safeString); 
    }; 
}]); 

plnkr

+0

我想在发送到后端时转义字符串。 – Hacker

+0

我不确定你要发布的内容。我假设你期待在你的输入字段的HTML,如果是这样的话[查看发布的问题](http://stackoverflow.com/questions/25940681/angularjs-strip-html-filter-on-ng-options) – SadiRubaiyet

0

(现在的问题是在自答案更新到不同的区域)不建议

使用PHP Sanitize Filters在服务器端过滤用户输入。

使用在客户端的任何卫生工具,因为该脚本可以被修改,以igno由恶意用户来重新制作。

+0

理想情况下,它应该在服务器端完成。如果我想在客户端做。我该怎么做? – Hacker

+0

在自定义表单验证器中使用$ sanitize。 –