Tech: Generating Dynamic HTML From JSON in Angular Apps

Not a new approach but fewer have done it

Khoi Bui
Webtips
5 min readMay 30, 2020

--

Angular developers often “hardcode” all HTML elements directly in the template. This seems fine for smaller components but becomes messy and repetitive when the same elements are expected to be rendered on the screen. Since Angular is an excellent framework for developing dynamic web pages, developers might consider inputting JSON configurations in the components in order to dynamically render HTML elements.

By building HTML from JSON configuration, we are able to:

  • keep the component DRY
  • save JSON configurations to database
  • reuse logic across the application

In this article, we will look at 3 simple examples demonstrating configuration based approach in order to build dynamic templates in Angular apps.

Example 1

In the first example, we need to display 3 buttons next to each other. Each button has its own attributes with different labels or icons. A naive approach to render these buttons is defining all of them in the template like below:

Naive approach

The above template should work with no problem; however, what if we need to render another set of 3 buttons somewhere else in our app or what if some of the button’s attributes need to be updated during run time? Tricky problems ain’t they?

With a configuration based approach we define the definitions for each button in JSON format then apply several Angular features such as data/property binding, structural directives (*ngIf, *ngFor) to help us manipulate DOM elements.

Config based approach

See full example in Stackblitz here. Plus, you can also apply this technique for other HTML elements, not just buttons. If you are not convinced by the configuration approach in the first example, let’s see if the next one will make you change the way you code.

Example 2

We will examine the table tag in this example. Unless you are using any existing libraries like Angular Material or agGrid, building a data table from scratch is such a conundrum. In order to render columns’ headers and display data for each cell, we will be using a lot of repeated tags which, in fact, can make our template super duper long. Let’s compare two solutions below and see which one is your favorite.

Naive approach

Config based approach

Notice that we don’t have to define a lot of <th> or <td> tags in config-based approach thanks to Angular structural directive *ngFor. In addition to that our data source can be fetched from remote servers which makes this approach superior to the other. See full code in Stackblitz here.

Example 3

In the last example, we are going to build a web page with a variety of HTML elements including h1, img and p. Similar to previous examples, two approaches will be shown for comparison purposes. See full code in Stackblitz here.

Naive approach

Config based approach

With a config-based approach, we might have to write more codes to fulfill the requirements so it’s not recommended for building simple web pages. This approach should only be used where you have to build a lot of pages that have a similar look and feel. It helps build the app with consistent layouts and definitely saves your team lots of time.

Wrap Up

After reading through 3 examples above I hope you will integrate config-based approach into your Angular apps. Don’t forget to “clap” if you found it helpful. Looking forward to seeing your opinions and suggestions about this awesome approach.

Did you see my other posts?

--

--

Khoi Bui
Webtips

Front End architect, opensource contributor and investment enthusiast. New content posted every week.