2013-08-31 23 views
1

有一个名称字段绑定到文本框。一切工作正常。 现在,当有人输入一个Unicode字符时,它将被转换为html元素。 在页面重新加载输入框中的名称显示html元素。这不应该是这样! 你如何解决这个问题?处理&数据绑定期间的数据

<input placeholder='Name' type="text" ng-model="form.firstName" ng-required="true" /> 

name = health &amp; wealth 
textbox = health &amp; wealth 
required in textbox = health & wealth 

回答

0

您可以使用过滤器进行格式化。

<input placeholder='Name' type="text" ng-model="form.firstName | html" ng-required="true" /> 

添加此过滤器:

angular.module("formatFilters", []).filter('html', function() { 
    function(input) { 
     return input.replace(/&amp;/g, "&"); 
    }; 
}); 

最后,不要忘了给模块添加此过滤器。

angular.module("app", ['formatFilters']) 
+0

我在数据加载和做任何事之前做过类似的事情。 http://stackoverflow.com/questions/3700326/decode-amp-back-to-in-javascript谢谢你的答案,但。 – Srinivas