question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enable html template usage with the directive

See original GitHub issue

I’m submitting a … (check one with “x”)

[ ] bug report => check the FAQ and search github for a similar issue or PR before submitting
[ ] support request => check the FAQ and search github for a similar issue before submitting
[x] feature request

Current behavior a html template like : "EXAMPLE": "<p>This is an example !</p><p>Look at this !</p>" won’t be rendered as html if used with the directive.

This : <div template>EXAMPLE</div>

Now render like this for the user : <p>This is an example !</p><p>Look at this !</p> (plain text)

Expected/desired behavior Make html renderable by the directive.

This : "EXAMPLE": "<p>This is an example !</p><p>Look at this !</p>" <div template>EXAMPLE</div>

Would render like this for the user :

This is an example !
Look at this !

Please tell us about your environment:

  • **ng2-translate version:**4.2.0

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

1reaction
renehamburgercommented, Dec 13, 2016

In some cases it may be necessary to sanitize the html content before it’s added to the DOM. This may be the case if the interpolation variables contain user-created content or if the translation string itself can be edited by outsiders, e.g., external translators.

Here’s a directive that sanitizes the html

import { Directive, ElementRef, Input, OnChanges, Sanitizer, SecurityContext,
  SimpleChanges } from '@angular/core';

// Sets the element's innerHTML to a sanitized version of [safeHtml]
@Directive({ selector: '[safeHtml]' })
export class HtmlDirective implements OnChanges {
  @Input() safeHtml: string;

  constructor(private elementRef: ElementRef, private sanitizer: Sanitizer) {}

  ngOnChanges(changes: SimpleChanges): any {
    if ('safeHtml' in changes) {
      this.elementRef.nativeElement.innerHTML =
        this.sanitizer.sanitize(SecurityContext.HTML, this.safeHtml);
    }
  }
}

to be used exactly the same way as innerHTML:

  <div [safeHtml]="'HELLO' | translate"></div>
0reactions
elgermcommented, Aug 12, 2020

When translations is changed the innerHTML isnt re rendered, is there a fix for this?

create a setter on the input() and run the changes from there

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTML Template Directives - Salesforce Developers
A directive is a special attribute that adds dynamic behavior to an HTML template. You can use certain directives on a root <template>...
Read more >
3.10 Using Template Directives
Use template directives to control how attributes that support substitution strings are processed.
Read more >
Built-in directives
If you have a static HTML <template> that you need to include in your Lit template, you can use the templateContent directive to...
Read more >
The Content Template element - HTML - MDN Web Docs
The <template> HTML element is a mechanism for holding HTML that is not to be rendered immediately when a page is loaded but...
Read more >
Template Directives Reference
A template directive could enable some compiler feature that makes your life ... You can also use set:html on a <Fragment> to avoid...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found