Angular2 Dynamic HTML with functional RouterLink

Reza Kajbaf

Long time user, first time question asker! I've been trying to figure this out for the better part of two days to no avail, so here we go.

Dynamic compiled templates from external sources in Angular2?

I am using http to get page content in the form of HTML from the WordPress API. So I have a template like this:

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

and "contentHTML" is a string variable in the component and assigned a value asynchronously via the API call to something like this:

    <a routerLink="/help/faq.html"> </a>

and I want that routerLink to work. Of course routerLink could be anything that's valid in a template.

If the above is not possible

If the above isn't going to work, what about a way to interpret the incoming HTML and add routerLinks on the fly to replace standard hrefs?

Adham Sabry

I came across the same issue, I followed this answer, but added a simple modification in the code where you import DynamicComponentModule, but added imports: [RouterModule]

So the steps would be as following install ng-dynamic:

npm install --save ng-dynamic

Then use the following import and code:

import { DynamicComponentModule } from 'ng-dynamic';

@NgModule({
   imports: [
       ...
       DynamicComponentModule.forRoot({imports: [RouterModule]}),
       ...
   ],
   ...
})
export class AppModule {} 

Then instead of:

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

Use:

<div *dynamicComponent="contentHTML"></div>

hope this helps!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Angular2 AOT for dynamic HTML

Angular2 add HTML to dynamic elements

Angular2 - Disable RouterLink

Dynamic routerLink value for ngFor item Angular 8

Dynamic routerLink in Angular application not returning correct result

Angular2 loading dynamic content/html in for loop

Binding a dynamic object to Html using Angular2

Dynamic data visualization in angular2 using HTML/angular2 template engine

angular2 routerLink not display on url

routerLink from Object in Angular2

Angular2: Pass boolean through routerLink

Angular2 Routerlink: add query parameters

Angular2 host: { '[routerLink]': '/foo' }

Handling Dynamic URL Params via RouterLink in Angular App

Using routerLink in Angular 2

Angular 2 Routerlink in for loop

routerLink is not working in angular 2

Angular 2 - should it be routerLink="theRoute" or [routerLink]="['the route']"?

Is it okay to add Angular routerLink to HTML <a> tags to avoid SEO issues?

Best way to pass angular routerLink URL's in an HTML string

Get routerLink properties from HTML in Angular unit test

Making routerLink conditional in Angular 4+ giving in HTML

use routerLink with parameter that is coming from promise in angular2

How to test routerLink inside button tag angular2

Angular2 beta getting exception "No provider for Router! (RouterLink -> Router)"

angular2 :host attr selector won't select [routerLink]

how to redirect or call routerlink in ts file in angular2

Angular2 set param of current route in routerLink

Angular2 - Adding routerLink parameters to chosen table value