Author avatar

Parul Saxena

Rendering Read-only Data with Angular

Parul Saxena

  • Jun 17, 2020
  • 7 Min read
  • Jun 17, 2020
  • 7 Min read
Languages Frameworks and Tools
Front End Web Developer
Client-side Frameworks


When developing things like application forms or dashboards with grids, you may want to render some data in read-only mode to the end user. You can do this in two ways:

  1. Use HTML label control
  2. Use Angular's readonly property

HTML label control is read-only by default, but it doesn't look compelling in form and grid controls. So, it's better to use Angular's Out Of the Box (OOB) readonly property. In this guide, we will apply the readonly property through a real-world scenario. We will create a small form that will allow users to create technical events on any website (for example,

To complete this guide, you must have the following:

  1. Node - 12.17.0
  2. Angular CLI - 9.0.2
  3. Visual Studio Code - 1.45.1

Getting Started

Creating an Angular Project

Execute the following command to create an Angular project.

ng new readonly-demo

Opening the Generated Application in Visual Studio Code

  1. Click on the File menu in the top menu bar and select the menu option Open Folder.
  2. In the file dialog box, navigate to the folder where you generated the application.
  3. In this folder, you should see a folder named readonly-demo. Select this folder and click Open in the file dialog box.

Setting up the Project

The next step is to install Twitter Bootstrap and jQuery and refer them in the project so you can use some of the form styles to make the form look good.

  1. Go to the command prompt and run the following command:

cd readonly-demo

  1. Run the following command to install jquery:

npm i jquery

  1. Run the following command to install Bootstrap:

npm i bootstrap

  1. In Visual Studio Code, open the file angular.json.

  2. Add the following line in the styles array before src/style.css:


  1. Add following lines in the scripts array:

"node_modules/jquery/dist/jquery.min.js", "node_modules/bootstrap/dist/js/bootstrap.min.js"

  1. Open the file src > app > app.module.ts, delete the existing content, and add the following code:
1import { BrowserModule } from '@angular/platform-browser';
2import { NgModule } from '@angular/core';
3import { FormsModule } from '@angular/forms';
4import { AppComponent } from './app.component';
7  declarations: [
8    AppComponent
9    ],
10  imports: [
11    BrowserModule,
12    FormsModule
13  ],
14  providers: [],
15  bootstrap: [AppComponent]
17export class AppModule { }

Now the project is all set to develop the form.

Developing the New Event Form

  1. Run the following command in the command prompt:

ng generate component create-event

  1. In Visual Studio Code, open the file src > app > create-event > create-event.component.ts , delete the contents of the file, and add following code:
1import { Component, OnInit } from '@angular/core';
4  selector: 'app-create-event',
5  templateUrl: './create-event.component.html',
6  styleUrls: ['./create-event.component.css']
8export class CreateEventComponent implements OnInit {
9  newEvent:any = {
10    baseUrl: ''
11  }
13  constructor() { }
15  ngOnInit(): void {
16  }
18  saveEvent(newEvent)
19  {
20    console.log(newEvent);
21  }
23  cancel()
24  {
26  }
  1. In Visual Studio Code, open the file src > app > create-event > create-event.component.html, delete the contents of the file, and add following code:
1<h1>New Event</h1>
3<div class="col-md-6">
4  <form #newEventForm="ngForm" (ngSubmit)="saveEvent(newEventForm.value)" autocomplete="off">
5    <div class="form-group">
6      <label for="eventName">Event Name:</label>
7      <input [(ngModel)]="" name="name" id="name" type="text" class="form-control" placeholder="Name of your event..." />
8    </div>
9    <div class="form-group">
10      <label for="eventDate">Event Date:</label>
11      <input [(ngModel)]="" name="date" id="eventDate" type="text" class="form-control" placeholder="format (mm/dd/yyyy)..." />
12    </div>
13    <div class="form-group">
14      <label for="eventTime">Event Time:</label>
15      <input [(ngModel)]="newEvent.time" name="time" id="eventTime" type="text" class="form-control" placeholder="start and end time..." />
16    </div>
17    <div class="form-group">
18      <label for="eventPrice">Event Price:</label>
19      <input [(ngModel)]="newEvent.price" name="price" id="eventPrice" type="text" type="number" class="form-control" placeholder="event price..." />
20    </div>
21    <div class="form-group" style="overflow: hidden;">
22      <label style="display: block;" for="onlineUrl">Online Url:</label>
23      <div style="float: left; width: 49%;">
24        <input type="text" [(ngModel)]="newEvent.baseUrl" name="baseUrl" id="baseUrl" class="form-control" placeholder="Base Url..."/>
25      </div>
26      <div style="float:right; width: 49%;">
27        <input [(ngModel)]="newEvent.onlineUrl" name="onlineUrl" id="onlineUrl" type="text" class="form-control" placeholder="Event Url..." />
28      </div>
29    </div>
30    <button type="submit" class="btn btn-primary">Save</button>
31    <button type="button" [disabled]="newEventForm.invalid" class="btn btn-default" (click)="cancel()">Cancel</button>
32  </form>
  1. In Visual Studio Code, open the file src > app > app.component.html, delete all the existing code, and add the following code:

Launching the Development Server

  1. Run the following command in the command prompt to start the Angular Development Server in watch mode: ng serve
  2. If prompted to share Angular CLI usage data, press the 'N' key to not share it.
  3. Open a browser and type the following URL in the address bar to launch the application: http://localhost:4200.
  4. Notice the Online URL field. The first text box is writable, and it should be rendered as read-only so users are not able to edit the base URL of the event. So let's do that.

Making the Base URL Read-Only

  1. In Visual Studio Code, open the file src > app > create-event > create-event.component.html.
  2. Go to line 24, and at the end of the element add the following code: [readonly]="true"

The final code should look like below:

<input type="text" [(ngModel)]="newEvent.baseUrl" name="baseUrl" id="baseUrl" class="form-control" placeholder="Base Url..." [readonly]="true"/>

  1. Go back to the application in the browser and observe that the base URL field is now read-only. Users cannot make any changes to it.


Congratulations!! You have learned:

  • How to render read-only data to end users in Angular
  • How to use Angular's property binding syntax to bind the HTML property of the input element

For more information, please refer to the following resources: