Good time,
I'm trying to make an application using Angular2.
Routing:
const appRoutes: Routes = [ {path: '', component: HomeComponent}, {path: 'first/:id', component: FirstComponent} ]; That is, when navigating to the site / first / ... address, control passes to the FirstComponent component and it can extract the id parameter. When prompted for site / first / item, the id parameter will be equal to "item" and so on.
Depending on this parameter, the component uses the service to send a request to the server to receive data. Based on the id parameter, the request address is generated. That is, in this case, the request will be for item.html. Data is a piece of html-code that will be inserted at a specific place in the template.
Question: how do I get this data in the form of html. So far, only a string is obtained, and it is inserted directly on the page along with the tags.
Service requesting the desired file:
import {Injectable} from '@angular/core'; import {Http} from '@angular/http'; @Injectable() export class HttpService{ constructor(private http: Http){ } getData(address: string){ return this.http.get(address); } } This is how I get the data in the component:
this.httpService.getData('data.html').subscribe((data: Response) => this.code=data.text()); I understand that the matter is in the used text () method, because it returns a string. How do I get the data correctly in the form of html, if in the requested file they are in the form of a simple html markup?
Do I need to use some other method of the Response class (which I don’t know about) or somehow process the resulting string (how?) Or use json (that is, do I need to somehow bring the source data to this format?)
Tell me please :) Thanks for the answers!