javascript - 数据绑定(bind)期间处理和输入数据

标签 javascript angularjs

有一个绑定(bind)到文本框的名称字段。一切正常。 现在,当有人输入 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'])

关于javascript - 数据绑定(bind)期间处理和输入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18545585/

相关文章:

angularjs - 自动包装 Material 中的大量项目

javascript - 我的 AngularJs 应用程序不会转发到另一个网站

angularjs - ng-include 编译为注释

javascript - 如何在 html 中调用 rootscope 函数 - Angularjs

javascript - 调用内部函数

javascript - React 切换类 + CSS 转换,不起作用……为什么?

javascript - Fabric js多选移动不会触发单个对象的移动事件

javascript - 工厂初始化期间的 Angular 图错误

javascript - 如何在 datatables jQuery 插件中指定列数

javascript - 使用 Ajax 传递单击的提交按钮值