简体   繁体   English

使用Angular将输入字段的字符串中的隐式编码HTML转换为纯文本

[英]Covert encoded HTML in string to plain text for Input Fields using Angular

I have a use case, where we can have '&#' characters inside of a JSON. 我有一个用例,其中我们可以在JSON中包含“&#”字符。

Name: "Kenneth Hinsvark & Maurice McAlister"

Address: "555555 W. Canyon Dr # B212"

The string values are pulled back from a database. 字符串值从数据库中拉回。 Apparently the values were saved to the DB with HTML encoding. 显然,这些值已使用HTML编码保存到数据库中。 I need to able to display the data in a textField without the HTML characters. 我需要能够在没有HTML字符的textField中显示数据。

My main requirement is that input fields values be converted to plain text. 我的主要要求是将输入字段的值转换为纯文本。

Name: <input type="text" ng-model="user.name" escape-to-plain-text></input>

Address: <input type="text" ng-model="user.address" escape-to-plain-text></input>

How can I translate the input values to plain text? 如何将输入值转换为纯文本?

Using $sce isn't working for me 使用$ sce对我不起作用

$scope.user.name = $sce.trustAsHtml('555555 W. Canyon Dr &#35; B212');

Working Code Example: 工作代码示例:

http://jsfiddle.net/egrendon/xa8cseoc/12/ http://jsfiddle.net/egrendon/xa8cseoc/12/

Right. 对。 So you need a couple of things: 因此,您需要注意以下几点:

  1. Inject ngSanitize into your module. ngSanitize注入模块。
  2. Do an ng-bind-html on the element you want to output the $sce result. 对要输出$sce结果的元素执行ng-bind-html

I've edited your fiddle here. 我在这里编辑了你的小提琴 - last line outputs the correct # character -最后一行输出正确的#字符

Because it outputs HTML, you'd need to grab the contents and parse them to put them in an input.. so maybe in that sense you'd be better off with just doing a match() on the text values to look for HTML entities. 因为它输出HTML,所以您需要获取内容并将其解析以将它们放入输入中。.因此,从这种意义上讲,最好对文本值执行match()以查找HTML会更好实体。

The ngSanitize solution works well for display fields but not for edit/input fields. ngSanitize解决方案适用于显示字段,但不适用于编辑/输入字段。

I was able to answer my primary question of converting HTML to plain text in a input field by creating a custom directive. 通过创建自定义指令,我能够回答将HTML转换为输入字段中的纯文本的主要问题。 Solution posted on js-fiddle 解决方案发布在js-fiddle上

http://jsfiddle.net/egrendon/xa8cseoc/22/ http://jsfiddle.net/egrendon/xa8cseoc/22/

var app = angular.module('myApp', ['ngSanitize']);

app.controller('LoginController', function ($scope, $sce) {
    // This object is fected from a DB. The data is stored DB this way....
    $scope.user = {
        name : "Kenneth Hinsvark &#38; Maurice McAlister",
        address : "555555 W. Canyon Dr &#35; B212"
    };

    $scope.sample = $sce.trustAsHtml('555555 W. Canyon Dr &#35; B212');
});

app.directive('escapeToPlainText', function () {
    return {
        require: 'ngModel',
        link : function(scope, element, attrs, ngModel) {

            scope.$watch(function(){return ngModel.$modelValue;}, function(newValue, oldValue){
                if (newValue && newValue.length > 0) {
                    var hasEncodedHTML = newValue.indexOf("&#") > -1;
                    if (hasEncodedHTML){
                        var encodedValue = newValue;
                        var decodedValue = decodeHTMLtoPlainText(encodedValue);

                        ngModel.$setViewValue(decodedValue);
                        ngModel.$render();
                        console.log(decodedValue);
                    }
                }
            }, true);


            function decodeHTMLtoPlainText(aValue) {
                var elem = document.createElement('div');
                elem.innerHTML = aValue;
                return elem.childNodes[0].nodeValue;
            }

        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM