1

We have few html pages.We are fetching content of those HTML pages from the database but tags are present in the file.We are using innerHtml (property binding) to render content into Html tags.

I would like to render the entire HTML page from the database in the Angular application.

Is there any way through which I can pull data from the backend alongside HTML tags and render those in Angular?

2
  • I'm not sure what you're asking if you could please clarify. Are you talking about wanting to import full html pages with their own html, body, meta tags, etc? As like an iframe? Or as their own page within the application that would still allow things like routing and angular lifecycle events etc etc? Because you wouldn't be able to just replace the angular attached html document with another and expect anything to work the same. Commented May 13, 2021 at 16:46
  • I have one service(returning JSON string with Key:Value) through which inner HTML contents are populated.but whenever there is a change then we need to change it in the database by the variable name. Instead of that I want to add one page/div in the database and fetch it from service and render in Angular component.So whenever there will be the change I have to take that HTML make changes/add new contents and update it in database.In this way there will be no impact on front end application. Commented May 13, 2021 at 16:59

3 Answers 3

0

I think if you pass json with the element type and attributes you can use Renderer2 to dynamically render the content you want. https://angular.io/api/core/Renderer2

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

Comments

0

I am able to do this by storing HTML content almost 20 div tags in database and fetching them from API and rendering on UI using innerHTML.

Comments

0

you need take account if your html data has styles or headers . I imagine you always can "extract" the inner html between <body> and <\body> using

const innerHTML=(data.match(/(?<=<body>)((.|\n)*?)(?=<\/body>)/) ||[""])[0];

data is the variable you has the whole html

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.