简体   繁体   English

如何显示自定义html标签?

[英]How to display custom html tag?

I'm trying to read user input text and display it. 我正在尝试阅读并显示用户输入的文本。

I've tried: 我试过了:

<div>
    <input type="text" ng-model="post.content" />
</div>

<div>
    <div ng-bind-html="post.content| htmlize"></div>
</div>

Script: 脚本:

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

app.controller('myController', function ($scope) {

    // controller implements...

}).value('HTMLIZE_CONVERSIONS', [

    { expr: /\n+?/g, value: '<br />' },
    { expr: /\[([BUI])\](.+)\[\/\1\]/g, value: '<$1>$2</$1>' }

]).filter('htmlize', function (HTMLIZE_CONVERSIONS) {

    return function (string) {
        return HTMLIZE_CONVERSIONS.reduce(function (result, conversion) {
            return result.replace(conversion.expr, conversion.value);
        }, string || '');

    };
});

When I type: 当我输入:

[B]bold[/B] [U]underline[/U] [I]italic[/I] [B]粗体[/ B] [U]下划线[/ U] [I]斜体[/ I]

it would convert to: 它将转换为:

bold u̲n̲d̲e̲r̲l̲i̲n̲e̲ italic 粗体 u̲n̲d̲e̲r̲l̲i̲n̲e̲ 斜体

My question: how to prevent the code detects other html tags? 我的问题:如何防止代码检测到其他html标签?

I've tried: 我试过了:

Case: 案件:

<script>alert('')</script>

Result: 结果:

Case: 案件:

<h1>text</h1>

Result: 结果:

text 文本

My goal: I just only want to convert [B][U][I] to <b><u><i> and display them as html tags (as I show), ortherwise; 我的目标:我只想将[B][U][I]<b><u><i>并将它们显示为html标签(如我所示);否则; display it as an plain text. 将其显示为纯文本。

How can I do that? 我怎样才能做到这一点?

You can 您可以

replace < with &lt; <替换为&lt;

replace > with &gt; &gt;替换>

to display < and > in plain text and then replace [ and ] characters with < and > to make them correct HTML tags 以纯文本显示<> ,然后用<>替换[]字符,使它们成为正确的HTML标记

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

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