Set the value of HTML input type radio to a nested JSON string

Currently, I'm developing an Angular application and encountering an issue where I am unable to access the nested array value 'subOption.name' for the input type radio's value. I'm uncertain if the error lies within the metaData structure or in the input type itself.

Data Structure

options: [
              {subQuestionTitle: 'Consult with SIG SMEs', subOption:[
                {name:'consult-sig-sme', key: '1',  value: 'impact'},
                {name:'consult-sig-sme', key: '2',  value: 'no-impact'},
                {name:'consult-sig-sme', key: '3',   value: 'not-reviewed'}
              ]},
              {subQuestionTitle: 'Plan Profile', subOption:[
                {name:'plan-profile', key: '1',  value: 'impact'},
                {name:'plan-profile', key: '2',  value: 'no-impact'},
                {name:'plan-profile', key: '3',   value: 'not-reviewed'}
              ]},
              {subQuestionTitle: 'Offshore or US?', subOption:[
                {name:'offshore', key: '1',  value: 'impact'},
                {name:'offshore', key: '2',  value: 'no-impact'},
                {name:'offshore', key: '3',   value: 'not-reviewed'}
              ]},
              {subQuestionTitle: 'Impacts & Awareness for IPMs & DCCs?', subOption:[
                {name:'impacts-and-awareness', key: '1',  value: 'impact'},
                {name:'impacts-and-awareness', key: '2',  value: 'no-impact'},
                {name:'impacts-and-awareness', key: '3',   value: 'not-reviewed'}
              ]}
    ],

Template Implementation

<div class="grid-row" *ngFor="let opt of question.options">
            <input type="radio" value="impact" [name]="opt.subOption.name"> 
            <input type="radio" value="no-impact" [name]="opt.subOption.name"> 
            <input type="radio" value="not-reviewed" [name]="opt.subOption.name"> 
</div>

Error Message

ERROR TypeError: Cannot read property 'subOption' of undefined

Upon modifying the code as follows:

<input type="radio" value="impact" [name]="opt.subOption">

The resulting implementation revealed in the source F12 is:

<input _ngcontent-c0="" type="radio" value="impact" name="[object Object],[object Object],[object Object]">

Answer №1

Give this a shot

<div class="grid-row" *ngFor="let opt of choices">
  {{opt.title}} <br/>
  <span *ngFor="let subopt of opt.subOptions">
    <input type="radio"[value]="subopt.value" [name]="subopt.name"/>
    {{subopt.value}}
  </span>
</div>

The most effective way to handle this would be by creating smaller components, for instance the content within the <span>...</span> could be its own SubOptionsComponent and each radio input along with the value could be encapsulated in a SubOptionComponent

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

Error occurred during JSON parsing: String in the JSON object is not properly terminated

Can someone help me with this problem I'm facing? I am trying to fetch a JSON object from my Node.js backend that is the result of a MySQL operation. Here's the code snippet: app.get('/get_events/:playgroundId', cors(), function(req,r ...

Generating JSON from shell variable and key-value pairs

Suppose I have the following variables declared in a bash script: mcD="had_a_farm" eei="eeieeio" declare -A animals=( ["duck"]="quack_quack" ["cow"]="moo_moo" ["pig"]="oink_oink" ) and I need to create the following JSON output: { "oldMcD": "had a farm" ...

Accessing an item within a JSON object using jQuery

Trying to access an element within a JSON object, part of the code is shown below: { " academy": { "business": { "E-commerce": [ I have successfully accessed the academy as the first element using the following code: $.getJSON("p ...

Utilize Javascript to refine JSON data strings

Currently, I am tackling a small project and facing a minor JS issue. The JSON string that I have looks like this: var jsObj = { "templates": { "form0": { "ID": "MyAlertNew", "isVisible": "true", ...

Tips for showcasing MySQL JSON data

There is a table in my database called purchase, where I insert data in JSON format. The JSON format contains multiple sets of data, for example, "products" has 2 items. https://i.stack.imgur.com/NJ5qz.png [{"products":["Asulak Silver 7","Print Gum MAP N ...

Having issues with Craco not recognizing alias configuration for TypeScript in Azure Pipeline webpack

I am encountering an issue with my ReactJs app that uses Craco, Webpack, and Typescript. While the application can run and build successfully locally, I am facing problems when trying to build it on Azure DevOps, specifically in creating aliases. azure ...

Aurelia's navigation feature adds "?id=5" to the URL instead of "/5"

I have set up my Aurelia Router in app.ts using the configureRouter function like this: configureRouter(config, router: Router) { config.map([ { route: ['users', 'users/:userId?'], na ...

There was an issue encountered while attempting to differentiate an object in the Angular datatable results. The data table only accepts arrays and iterables

Looking to retrieve user data from an API using the httpClient post method in Angular 5, I faced a challenge as I couldn't properly display the retrieved information in a datatable. Below are snippets of the code that I have experimented with: User ...

What is the process for transferring a Pulumi Output<T> to the container definition of a task in ECS?

When creating a generic ECS service that deals with dynamic data, it is important to note that the containerDefinition within a Task Definition must be provided as a single valid JSON document. The code snippet for this setup looks like: genericClientServi ...

Unable to serve static files when using NextJs in conjunction with Storybook

The documentation for Next.js (found here) suggests placing image file paths under the public directory. I have a component that successfully displays an image in my Next.js project, but it doesn't render properly within Storybook. The image file is ...

Exploring the world of fetching JSON data with JavaScript

Currently working on creating JMAKI widgets, my goal involves showcasing json data on an html page utilizing javascript. In your opinion, what is the most effective method for accessing json data using javascript? ...

The image hover feature is not functioning as expected in Angular 4

Currently, I am involved in a project using Angular 4. One particular section involves changing images on hover. Although I have implemented the following code, it does not seem to be functioning correctly for me. Interestingly, the same code works perfect ...

Angular 7: module not found error - unable to locate 'underscore' package

Currently tackling a project using Angular 7. Attempted to add npm install --save @types/underscore but encountered the following errors: npm WARN @agm/[email protected] requires a peer of @angular/common@^5.0.0 || ^6.0.0 but none is installe ...

Determining the sequence of events for @HostListener event within an Angular directive

I am facing an issue with my Angular 9 application wherein a parent component contains a child component (referred to as "overview") which has a button. Upon clicking the button, the child component emits an event that should be handled by the parent compo ...

Exploring a JSON Object in TypeScript: A Step-by-Step Guide

I am currently utilizing Angular 7 and have a query that returns JSON data with a specific format: [ { "text": "test 1", "value": "1", "nbr": "1", "children": [ { "text": "test 1_1", ...

Navigating through various JSON arrays using Angular

I am currently working with a large JSON file in Angular and trying to iterate through it. The structure of the JSON file is as follows: { "subject1":[ { "title":"titlehere", "info":"infohere." }], ...

In React and TypeScript, when I pass a state as a prop, the data fails to render

Here is the useState function for managing the Data Interestingly, removing this state from the code doesn't affect rendering at all. const [cart, setCart] = useState([] as Product[]); This piece of code handles Mapping and Rendering the Data <Sin ...

Styling child elements in Angular using css from its parent element

I have a question: Regarding the structure below <component-parent> <first-child> <second-child> <third-child></third-child> </second-child> </first-child> </component-parent> Now I want t ...

What is the best way to extract and store data from a Json String that I have received as a response?

{"OFFERS":[{"OFFER_ID":"490","OFFER_IMAGE":"http://www.discountbox.in/DiscountBoxUpload/19007012.JPG","OFFER":"Clearence Stock"},{"OFFER_ID":"488","OFFER_IMAGE":"http://www.discountbox.in/DiscountBoxUpload/36911135.JPG","OFFER":"Upto 20% Off"},{"OFFER_ID": ...

Constantly loading image with Meteor HTTP request

Within my Meteor application, I am attempting to dynamically load a random image from an API which returns JSON data structured like this: { "id":2026 "url": "https:// ... " , "large_url":null, "source_id":609, "copyright":"CC0", "site":"unsplash" } ...