手柄&在数据绑定期间的数据中

handle & in data during data binding

本文关键字:amp 数据 数据绑定 手柄      更新时间:2023-09-26

有一个绑定到文本框的名称字段。一切都很好。现在,当有人输入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

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

<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'])