Angular HTML binding

后端 未结 21 2615
暖寄归人
暖寄归人 2020-11-21 04:00

I am writing an Angular application and I have an HTML response I want to display.

How do I do that? If I simply use the binding syntax {{myVal}} it en

相关标签:
21条回答
  • 2020-11-21 05:03

    You can use several approaches to achieve the solution. As already said in the approved answer, you can use:

    <div [innerHTML]="myVal"></div>
    

    depending on what you are trying to achieve, you can also try other things like javascript DOM (not recommended, DOM operations are slow):

    Presentation

    <div id="test"></test>
    

    Component

    var p = document.getElementsById("test");
    p.outerHTML = myVal;
    

    Property Binding

    Javascript DOM Outer HTML

    0 讨论(0)
  • 2020-11-21 05:04

    Just simply use [innerHTML] attribute in your HTML, something like this below:

    <div [innerHTML]="myVal"></div>
    

    Ever had properties in your component that contain some html markup or entities that you need to display in your template? The traditional interpolation won't work, but the innerHTML property binding comes to the rescue.

    Using {{myVal}} Does NOT work as expected! This won't pick up the HTML tags like <p>, <strong> etc and pass it only as strings...

    Imagine you have this code in your component:

    const myVal:string ='<strong>Stackoverflow</strong> is <em>helpful!</em>'

    If you use {{myVal}}, you will get this in the view:

    <strong>Stackoverflow</strong> is <em>helpful!</em>
    

    but using [innerHTML]="myVal"makes the result as expected like this:

    Stackoverflow is helpful!

    0 讨论(0)
  • 2020-11-21 05:05

    Short answer was provided here already: use <div [innerHTML]="yourHtml"> binding.

    However the rest of the advices mentioned here might be misleading. Angular has a built-in sanitizing mechanism when you bind to properties like that. Since Angular is not a dedicated sanitizing library, it is overzealous towards suspicious content to not take any risks. For example, it sanitizes all SVG content into empty string.

    You might hear advices to "sanitize" your content by using DomSanitizer to mark content as safe with bypassSecurityTrustXXX methods. There are also suggestions to use pipe to do that and that pipe is often called safeHtml.

    All of this is misleading because it actually bypasses sanitizing, not sanitizing your content. This could be a security concern because if you ever do this on user provided content or on anything that you are not sure about — you open yourself up for a malicious code attacks.

    If Angular removes something that you need by its built-in sanitization — what you can do instead of disabling it is delegate actual sanitization to a dedicated library that is good at that task. For example — DOMPurify.

    I've made a wrapper library for it so it could be easily used with Angular: https://github.com/TinkoffCreditSystems/ng-dompurify

    It also has a pipe to declaratively sanitize HTML:

    <div [innerHtml]="value | dompurify"></div>
    

    The difference to pipes suggested here is that it actually does do the sanitization through DOMPurify and therefore work for SVG.

    One thing to keep in mind is DOMPurify is great for sanitizing HTML/SVG, but not CSS. So you can provider Angular's CSS sanitizer to handle CSS:

    import {NgModule, ɵ_sanitizeStyle} from '@angular/core';
    import {SANITIZE_STYLE} from '@tinkoff/ng-dompurify';
    
    @NgModule({
        // ...
        providers: [
            {
                provide: SANITIZE_STYLE,
                useValue: ɵ_sanitizeStyle,
            },
        ],
        // ...
    })
    export class AppModule {}
    

    It's internal — hense ɵ prefix, but this is how Angular team use it across their own packages as well anyway. That library also works for Angular Universal and server side renedring environment.

    0 讨论(0)
提交回复
热议问题