Angular 8/7 | Radio Value Selection and Getting Updated Object in *ngFor Listing Example

In Angular, listing radio form controls and setting their behavior becomes tricky sometimes. There is a number of things a developer wants to do with a list of radio controls which are usually populated dynamically using an Object with JSON from the server-side.

Listing of items in Angular is done using *ngFor directive which is used to iterate over the values in an array or object.

Here we will discuss Radio control listing using a dynamic set of data in JSON format with a demo of the User Access Management page with a very simple explanation.

In our Radio list, we will try to achieve some basic things:

  1. Generate a Radio list using *ngFor with Object passed.
  2. Check a radio control by passing a value from a group of radios in a single row with the same name.
  3.  Check all radios in a single column of a table using a header master radio button.
  4. Get updated object of user access with changes radio value to update on the server-side.

What is a User Access System?

User Access System is created in system software to allow a specific user some access types like Read, Read-Write or No Access. UAS limits unauthorized access to some module(s) which is not supposed to be handled by any person.

Here we are not going to work on any real UAS, here we are just creating a table with  Radio controls that’s it 🙂

Let’s get started!

Without spending much time on creating a new project, go ahead with any of you Angular 2 plus version based project.

JSON response from server

Suppose we have been provided with following JSON object from server-side by making an HTTP call:

In the above JSON object we have modules and then submodules with their own IDs as moduleId and subModuleId respectively.

The selectedRightType property is having a value of Right which we need to show as selected/ checked by default as list loads.

HTML Template to Show this list

In our HTML template, we will have two *ngFor directive first one will traverse over Modules and the second one will traverse all Submodules in each module.

On Modules table header Radios we will have [(ngModel)], (change), [value] and name properties.

In submodule table rows, each row will have radio control with [checked], [(ngModel)], [value] and name property. Here we added [checked] input property to check radio-based on the server response.

Finally out HTML template will look like this:

Now after making changes on radio selection you will get updated object in modules_object due to [(ngModel)].

Here you need to make sure the name of radio controls in a single row must match to create a group of them to select only single value.

Also here we used [value] instead of on value to make communication possible between [(ngModel)].

That’s it now you are done with a dynamic listing of Radio controls using JSON object. Let me know in the comments if you face any issue implementing this.

Leave a Reply

1 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
0 Comment authors
Recent comment authors
newest oldest most voted
Notify of

[…] Also Check: Angular 8/7 | Radio Value Selection and Getting Updated Object in *ngFor Listing Example […]