2

Is there a way to include an external html file, I've been unable to use templateUrl, it keeps appending the base to the start of the url.

@Component({
  selector: 'enroll-header',
  templateUrl: 'http://blank.blank.com/somecool.html'
})

it'll try to find it at "./http://blank.blank.com/somecool.html"

and fail

1
  • You might be looking for ComponentFactoryResolver which includes links to a plnkr and downloadable code. Commented Apr 17, 2017 at 10:07

4 Answers 4

3

Using an external URL in templateURL is not supported (possibly because it exposes you to security risks). As a workaround you can use a template with your component that binds to and displays a single variable. Then you can set the variable equal to the html you want to render. Please check these two similar SO questions:

Sign up to request clarification or add additional context in comments.

Comments

2

I believe all of the templateUrl are relative to the root of your application, so I don't think this would work.

Comments

0

I ended up using a scrape job to get the new html files, and then addded a serverside include command in my index.html file to append them.

Comments

-1
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);
        });
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.