Angular4 Load external html page in a div

2020-01-29 08:25发布

问题:

I had an application in angular1 and I was using jquery to load an external page in my HTML element. This is the html of my container element:

<div id="externalContainer" class="text-center">
</div>

I used the following script to load the external page.

$( "#externalContainer" ).load( myExternalPageLink );

Now this doesn't seem to work in angular4, can experts guide me how can I achieve this functionality in angular4?

回答1:

You can use [innerHtml] something like

<div [innerHtml]="myTemplate">
</div>

In your component

export public class MyComponent {
    private myTemplate: any = "";
    constructor(http: Http) {
        http.get(myExternalPageLink).map((html:any) => this.myTemplate = html);
    }
}


回答2:

I know that I am a little late to the party here, but I came across this question while trying to solve my own problem and wanted to share in case someone else had the same issue.

I was trying to also allow custom components to be used in our external html for our blog.

I documented it all on our blog (dynamically generate angular components from external html).

Hopefully this is moderately helpful to the next person who comes across this.