简体   繁体   中英

Angular2: Load content of html page into a variable

So I have an html page located in this path:

./Help/HelpMenu/How-To-Bake-Pies.html

I'm trying to load the contents of this html page into a variable of type string like so:

private _content: string = '';

this.http.get("./Help/HelpMenu/How-To-Bake-Pies.html").map((html:any) => this._content = html);

After the second statement is executed the _content variable remains empty, am I missing something?

You need to subscribe to the observable otherwise it won't get executed - in other words, the HTTP call will never happen

this.http
.get("./Help/HelpMenu/How-To-Bake-Pies.html")
.subscribe((html:any) => this._content = html);

map does not subscribe to the observable. It is used to manipulate data. map returns a new observable that you can subscribe to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM