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() {
//Loads the HTML and bind it to the view
this.
http.
get(this.templateUrl).
map(res => this.parseTemplate(res)).
catch(this.handleTempalteError.bind(this)).subscribe(res => {
console.log(res);
});
}
}
задан Aviv Oron 25 March 2019 в 15:10
поделиться