Is there any alternate way in angular to achieve what ng-include does in angularjs?
            Asked
            
        
        
            Active
            
        
            Viewed 2.3k times
        
    16
            
            
        - 
                    http://stackoverflow.com/questions/39328215/angular2-dynamic-template-or-ng-include check this question, Its duplicated. – Aswin KV Apr 16 '17 at 19:10
3 Answers
5
            
            
        The closest to ng-include is ngTemplateOutlet directive. You need to pass a TemplateRef to it and optional context. Something like that:
@Component({
  selector: 'child',
  template: `
    <div>
      here is child template that includes myTemplate
      <ng-container [ngTemplateOutlet]="myTemplate"></ng-container>
    </div>`
})
export class ChildComponent {
  @Input() myTemplate: TemplateRef<any>;
}
@Component({
  selector: 'app-root',
  template: `
    <p>Parent</p>
    <child [myTemplate]="myTemplate"></child>
    <ng-template #myTemplate>hi julia template!</ng-template>
  `
})
export class AppComponent {
  @ViewChild('myTemplate', {read: TemplateRef}) myTemplate: TemplateRef<any>;
}
- Parent component querys the template and passes it to the child component
- Child component uses ngTemplateOutlet directive to create view and render it.
 
    
    
        Julia Passynkova
        
- 17,256
- 6
- 33
- 32
2
            
            
        Thinking from angular2+ way it's better to declare the child template as component:
@Component({
  selector: 'app-child', 
  template: `
    <ng-container>
      here is child template that includes myTemplate
    </ng-container>`
})
export class ChildComponent {
}
@Component({
  selector: 'app-root',
  template: `
    <p>Parent</p>
    <app-child ></app-child>
  `
})
export class AppComponent {
}
 
    
    
        Bogdan
        
- 656
- 15
- 25
- 
                    I like this approach since it treats components and child components in the same consistent way. – chillijay Oct 11 '22 at 22:30
1
            
            
        //ng-include equivalent in Angular2/4
// How to create directive for ng-clude in Angular2/4
import {
    Directive,
    ElementRef,
    Input,
    OnInit
} from '@angular/core';
import {
    Headers,
    Http,
    Response
} from '@angular/http';
@Directive({
    selector: 'ngInclude'
})
export class NgIncludeDirective implements OnInit {
    @Input('src')
    private templateUrl: string;
    @Input('type')
    private type: string;
    constructor(private element: ElementRef, private http: Http) {
    }
    parseTemplate(res: Response) {
        if (this.type == 'template') {
            this.element.nativeElement.innerHTML = res.text();
        } else if (this.type == 'style') {
            var head = document.head || document.getElementsByTagName('head')[0];
            var style = document.createElement('style');
            style.type = 'text/css';
            style.appendChild(document.createTextNode(res.text()));
            head.appendChild(style);
        }
    }
    handleTempalteError(err) {
    }
    ngOnInit() {
        this.
        http.
        get(this.templateUrl).
        map(res => this.parseTemplate(res)).
        catch(this.handleTempalteError.bind(this)).subscribe(res => {
            console.log(res);
        });
    }
}
enter code here
    // html code
    <
    ngInclude src = "{{src}}"
type = "template" > < /ngInclude>
 
    
    
        Sibiraj
        
- 4,486
- 7
- 33
- 57
 
    
    
        Akshay Bohra
        
- 323
- 3
- 6
 
    