Using Angular 6 to load external HTML files with CSS inside a router-outlet

I'm currently working on creating a json-based dynamic template using Angular 6.

There are certain scenarios where I need to fetch external html content (which could be stored in a database) along with its corresponding css (also stored separately in the database as text) to use in a router-outlet component in my application.

For example, I have a home.html and home.ts file that need to be linked through routing. Instead of having the html code directly in the home.html file, I want to pull it from the database and display it. Additionally, I want to retrieve the css code from the database without embedding it directly in the html.

Furthermore, I require the JavaScript functions for the dynamic html content (such as a delete link or cancel link) to be stored in a separate file, similar to TypeScript. Is it possible to load TypeScript files dynamically as well?

const routes: Routes = [
  { path: '', redirectTo: 'home', pathMatch: 'full' }
]

One possible approach I am considering is outlined in the following link: How do I load an HTML page in a <div> using JavaScript?

However, I am exploring if there are better practices or approaches specifically tailored for Angular 6 that could be used in this scenario.

Answer №1

I believe the method recommended in the provided link is an effective way to accomplish this task. Additionally, you have the option of utilizing the angular innerHtml property.

<div class="home" [innerHtml]="testhtml"></div>

In this scenario, testhtml represents the html data retrieved from the database. For further details, refer to this source.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Filtering data on objects in Angular can be achieved by utilizing the built-in

Retrieving data from the backend using this function: private fetchData(): void { this.dataService.fetchData().pipe( tap((response: any) => { this.persons = response.results; this.familyMembersTrue = this.persons.filter(x =&g ...

Choose items that do not contain ::before or ::after pseudo-elements

I am looking to apply a specific font style to all text on a page except for Font Icons (specifically Font Awesome) which do not share a common class. In order to achieve this, I need to target elements in one of the following ways: Elements that do not ...

Troubleshooting inactive CSS hover animation

Greetings! I'm facing an issue with a CSS hover animation. Here are two demos I've created: In the first DEMO, the animation works perfectly. However, in the second DEMO, it doesn't seem to be functioning. On the second demo, there are two ...

My divs seem to overlap in strange, unpredictable ways depending on the zoom level - it's an odd phenomenon that occurs

This situation is causing me frustration... depending on the zoom level of the browser, my boxes do not align properly and it ends up looking messy (in all browsers). Any advice? Could I handle this more effectively with jQuery? HTML </div> <div ...

Testing the GET method in an Angular service: A guide

I'm currently facing an issue with my service method and unit test setup. Despite writing a unit test for the getter method, the coverage report indicates that this method is not covered. I would appreciate any advice on what might be going wrong in m ...

How to retrieve the value of a selected radio button in an AngularJS radio button group that uses ng-repeat

In the following code snippet, I am trying to retrieve the value when any of the radio buttons is selected: <label ng-repeat="SurveyType in SurveyTypes"> <input type="radio" name="SurveyTypeName" ng-model="surveyData.SurveyTypeN ...

Angular Typescript can return an object when the toString method is called

When I make an http get call, I receive a client in the following way: getClientById(clientId): Observable<Client>{ let params = new HttpParams().set('clientId', clientId); return this.httpClient.get<Client>('http:// ...

ng-view is the culprit behind the website's fatal error

Encountering a "RangeError: Maximum call stack size exceeded" in the console while trying to recreate a basic routing example from w3schools. The crash seems to be linked to <div ng-view></div> in index.html. Despite making minimal changes from ...

Error in Displaying Vuetify Child Router View

I am currently working on integrating a child router-view to be displayed alongside surrounding components. Here is an overview of my routing setup: { path: "/login", name: "TheLoginView", component: TheLoginView, }, { path: "/dashboa ...

The debounce function seems to be malfunctioning as I attempt to refine search results by typing in the input field

Currently, I am attempting to implement a filter for my search results using debounce lodash. Although the filtering functionality is working, the debounce feature seems to be malfunctioning. Whenever I input text into the search bar, an API call is trigge ...

Unable to retrieve information from the redux store

I'm having trouble accessing a property from a Redux store while working with Angular. Within the IApp interface, there are two properties: one is an interface and the other is a number. interface AppStoreInterface { layoutInterface: LayoutInterfa ...

Ways to enable components to access a string dependency token

I'm currently developing an Angular application that utilizes Angular Universal for server-side rendering functionality. One interesting aspect of my project involves passing a string dependency token as a provider within the providers array in serve ...

Bootstrap Button Problem: Difficulty arranging buttons in a neat way, unable to position them next to each other

I'm currently working on a real estate website project and have designed a Photoshop template which is uploaded on Behance. Check it out here. Right now, I'm creating the static version of the real estate store template and facing an issue with ...

Is there a way to divide all the characters within a string, excluding the sequence " "?

My issue (resolved) I've been working on an animation where each letter appears sequentially, but I encountered a problem. In order to transform the letters properly, I had to wrap my span tags in a flex div because using inline-block didn't all ...

React is failing to display identical values for each item being mapped in the same sequence

I have implemented some standard mapping logic. {MEMBERSHIPS.map((mItem, index) => ( <TableCell className="text-uppercase text-center" colSpan={2} padding="dense" ...

Nav Bar Toggle Button - Refuses to display the dropdown menus

My Django homepage features a Bootstrap navbar that, when resized, displays a toggle button. However, I am experiencing difficulty with dropdown functionality for the navbar items once the toggle button is activated. Can anyone provide guidance on how to a ...

What is the best way to display JSON response as code instead of a string in AngularJS?

When retrieving my article from the database as a JSON object, I encounter an issue with the body content. The HTML codes in the body are displayed as strings within double quotation marks by AngularJS. How can I resolve this? Angular controller snippet: ...

The DateFormat.js script encountered an error: Unexpected token export

While working with some Kubernetes PODS, I suddenly encountered an error in one of the pods. The issue seems to be originating from line 6 of my code. I haven't made any changes recently; I was just deploying on GitLab when I noticed that this particu ...

"Encountering issues with NPM run build not working during Docker build process

Encountering an error while running docker build is causing frustration. Docker runs npm install -f, and changing it to npm install did not solve the problem. The lengthy logs cannot be posted here, but can be viewed in detail here. Here's a snippet o ...

Issue with the deprecated router package in Angular 2 rc3

Welcome Angular 2 rc.3! After following the instructions in the 5 Min Quickstart and using project.json to configure my project, I encountered an error when running npm install: No compatible version found: @angular/<a href="/cdn-cgi/l/email-protectio ...