有一个绑定(bind)到文本框的名称字段。一切正常。 现在,当有人输入 unicode 字符时,它会转换为 html 元素。 在页面重新加载时,输入框中的名称显示 html 元素。事实不应该如此! 你如何解决这个问题?
<input placeholder='Name' type="text" ng-model="form.firstName" ng-required="true" />
name = health & wealth
textbox = health & wealth
required in textbox = health & wealth
最佳答案
您可以使用过滤器对其进行格式化。
<input placeholder='Name' type="text" ng-model="form.firstName | html" ng-required="true" />
添加此过滤器:
angular.module("formatFilters", []).filter('html', function() {
function(input) {
return input.replace(/&/g, "&");
};
});
最后,不要忘记将此过滤器添加到模块中。
angular.module("app", ['formatFilters'])
关于javascript - 数据绑定(bind)期间处理和输入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18545585/