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.

innerHTML ignores my angular directives

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

I have the below translation token (includes HTML tag with an angular directive) "MSG":"You may <a routerLink='/login'> Sign in </a> again"

and my component HTML is <div [innerHTML]="'MSG' | translate"></div>

The final translation strips of the routerLink directive

Expected/desired behavior The final translation should have routerLink directive

ng2-translate version: 6.0.1

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:19

github_iconTop GitHub Comments

2reactions
vadostcommented, Jul 27, 2017

import { AfterViewInit, Directive, ElementRef, Input, OnDestroy, Renderer2 } from ‘@angular/core’; import { TranslateService } from ‘translate-core’; import { Router } from ‘@angular/router’;

@Directive({ selector: ‘[appTranslate]’ }) export class TranslateDirective implements AfterViewInit, OnDestroy { @Input() appTranslate: string; listenClickFunc: Function;

constructor(private element: ElementRef, private translate: TranslateService, private router: Router, private renderer: Renderer2) {}

ngAfterViewInit() { this.translate.get(this.appTranslate).subscribe((translateText: string) => { this.element.nativeElement.innerHTML = translateText; // this.cdRef.detectChanges();

  const navigationElements = Array.prototype.slice.call(this.element.nativeElement.querySelectorAll('a[routerLink]'));
  navigationElements.forEach(elem => {
    this.listenClickFunc = this.renderer.listen(elem, 'click', (event) => {
      event.preventDefault();
      this.router.navigate([elem.getAttribute('routerLink')]);
    });
  });
});

}

ngOnDestroy() { this.listenClickFunc(); }

}

1reaction
voice-technologiescommented, May 3, 2018

So, to be clear, does this mean that an existing, working angularJS app that features custom directives inserted by ng-bind-html, then compiled with $compile, cannot be upgraded to angular 2+? And that there isn’t even a workaround? Given that for back-compatibilty reasons the inserted HTML content cannot be changed, is the app now stuck with the end-of-life angularJS forever? I’m sorry, but that seems to be a major hole in angular, which I would hope to see addressed in a release sooner rather than later.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 2: How can I apply directives to sanitized html/innerhtml
I would like to create a directive that is applied to the innerhtml, but while the html is displayed, it is not compiled...
Read more >
How To Use the innerHTML Property Binding in Angular
Angular 2+ supports an [innerHTML] property binding that will render HTML. If you were to otherwise use interpolation, it would be treated ...
Read more >
Reapplying an Angular Directive on DOM Changes - Medium
We want this behavior to be added by an Angular Directive. But a Directive will not be re-executed unless the element it is...
Read more >
Using Angular innerHtml to display user-generated content ...
The issue is that DomSanitizer is stripping too much out of my HTML, and there's no way to configure its rules. In my...
Read more >
Template syntax - Angular
To eliminate the risk of script injection attacks, Angular does not support the <script> element in templates. Angular ignores the <script> tag and...
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