Dynamically load HTML template in angular2

后端 未结 5 1466
余生分开走
余生分开走 2020-11-28 06:54

I have created a project using angular-cli which contains AppComponent as follows:

import { Component } from \'@angular/core\';         


        
相关标签:
5条回答
  • 2020-11-28 06:57

    You could use [innerHtml] in a my-template component with something like this (I didn't test) :

    @Component({
        selector: 'my-template',
        template: `
    <div [innerHtml]="myTemplate">
    </div>
    `})
    export public class MyTemplate {
        private myTemplate: any = "";
        @Input() url: string;
        constructor(http: Http) {
            http.get("/path-to-your-jsp").map((html:any) => this.myTemplate = html);
        }
    }
    
    0 讨论(0)
  • 2020-11-28 07:08

    To interpolate a template with some Good Morning, {{title}}, you may use Suguru Inatomi's "ng-dynamic" component.

    First you have to install it :

    npm install --save ng-dynamic
    

    Then import into your NgModule :

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

    Finally use it like this :

    @Component({
      selector: 'app-root',
      template: '<div *dynamicComponent="template; context: bindings;"></div>'
    })
    export class AppComponent {
      bindings: any = {title: "Chuck Norris"};
      template: string = `
        <h1>Good Morning, {{title}}</h1>
      `;
      constructor(http: Http) {
        http.get("/path-to-your-jsp").map((html:string) => this.template = html); //<- You may set bindings in request headers...
      }
    }
    

    You could use components in your template by defining a SharedModule. I added a custom "my-button" with succes like in the documentation example here : https://github.com/laco0416/ng-dynamic

    0 讨论(0)
  • 2020-11-28 07:15

    Why not have all the template in one file and display them according to the condition.. Like your ./app.component.html will look like:

    <div *ngIf="isWorld" >
      <h1>  Hello World </h1>
    </div>
    <div *ngIf="isUniverse" >
      <h1>  Hello Universe </h1>
    </div>
    

    Any idea on its effect on build time/size?

    0 讨论(0)
  • 2020-11-28 07:17

    It appears that the way to do this now is to set the responseType when you make your request. HttpClient-Requesting non-JSON data `

    @Component({
      selector: 'my-template',
      template: '<div [innerHtml]="myTemplate"></div>'
    })
    export public class MyTemplate {
      private myTemplate: any = "";
      @Input() url: string;
      constructor(http: Http) {
        http.get("/path-to-your-jsp", { responseType: 'text' })
          .subscribe(
            (data: string) => {
              this.myTemplate = html;
            }
          );
      }
    }
    

    `

    0 讨论(0)
  • 2020-11-28 07:17

    worked with angular 6

    import { Component, Input } from '@angular/core';
    import { HttpClient } from '@angular/common/http';
    
    @Component({
      selector: 'app-root',
      template: `
                <div [innerHtml]="myTemplate">
                </div>
              `,
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
      private myTemplate: any = '';
      constructor(http: HttpClient) {
        http.get('/service-path', {responseType: 'text'}).subscribe(data => this.myTemplate = data);
      }
    }
    
    0 讨论(0)
提交回复
热议问题