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

Converting multiple WordPress plugins into a single JSON option

Having a plugin with numerous options, I am utilizing an angular application on the frontend to display them all. In order to store these options in the database as JSON data, I need to ensure that all 10 options are saved within a single row in the wp_opt ...

Is it possible to apply JavaScript object destructuring but make changes to certain values before assigning them to a new object?

After receiving movie data from an api, I am currently manually creating a new object with a subset of properties and modified values. Is there a more efficient way to achieve this using javascript/typescript object destructuring syntax? I specifically wa ...

Issue with Typescript not recognizing default properties on components

Can someone help me troubleshoot the issue I'm encountering in this code snippet: export type PackageLanguage = "de" | "en"; export interface ICookieConsentProps { language?: PackageLanguage ; } function CookieConsent({ langua ...

Agents should only be initialized within a before function or a spec in Angular

Having trouble with my private function: private GetChargesByClient(clientId: number): Observable<any[]> { const ds = new Date(); const dateTocompare = new Date(ds.setFullYear(ds.getFullYear() - 1)); return this.getCharges(id).pipe(map(res => re ...

Encountered a resource loading error while launching a Spring-Boot Application with an Angular 7 Frontend due to missing resources

tag, I am eager to construct a Spring-Boot-Application with an Angular frontend. Initially, I utilized the Spring-Boot-Initializer to establish a project and incorporated a RestController for /greet/world. Following that, in my src/main-folder, I initiate ...

Is there a way to automatically validate v-forms inside a v-data-table when the page loads?

In my data entry form, I have utilized a v-data-table with each column containing a v-form and v-text-field for direct value updates. My goal is to validate all fields upon page load to identify any incorrect data inputs. However, I am facing challenges in ...

Combine all TypeScript enums into a single one

Looking for a way to combine two separate enums into one for easier use. export enum ActionTypes1 { ClearError = 'CLEAR_ERROR', PrependError = 'PREPEND_ERROR', } export enum ActionTypes2 { IncrementCounter = 'INCREMENT_COUNT ...

Retrieve data from TypeScript file (.ts) and use it in an HTML document

Recently I started learning Typescript and HTML as I work on building an Angular2 application. At the moment, I have a TypeScript file that resembles the following structure: import {Http, Headers} from 'angular2/http'; import {Component} from & ...

Issue with Angular 2 HTTP provider: Observable subscription not triggering

I'm struggling to trigger the .subscribe() method on an observable in Angular 2. I have a provider that uses the Http Service to fetch data and return an observable for the controller to subscribe to. I can't figure out why the subscribe functio ...

When attempting to send JSON data using Ajax, you may encounter an issue such as receiving an error message

I am currently working on developing a login with Facebook application using Codeigniter. My challenge is passing JSON data to my controller through AJAX. This is the code snippet I am using: var data = JSON.stringify(response); alert(data); $.ajax ...

Dealing with the complexities of Jquery Ajax and WCF Webservice integration:

I have developed a WCF Webservice method that accepts two parameters, both strings, and returns an XmlElement. Currently, I am encountering issues with an ASP.NET page that contains a JQuery AJAX call to this method. Despite trying to troubleshoot using o ...

Is there a way to define an interface that consists of child objects as the type for a function that uses destructured props?

Is there an alternative to this method? import { client } from "./setupApi"; export const getLayout = ({ page, entity, list }: {page: string, entity: string, list: string}) => { return client.get("/secure/nav.json"); }; How do I ...

Retrieving information from JSON documents with Marklogic 9

The following is the structure of the document: { "sNo": 0, "id": 31575378, "title": "Sustained preventive chemotherapy", "journal": "Infectious diseases of poverty", "publ_date": "2019-10-03", "species" ...

What is the best way to incorporate "thread.sleep" in an Angular 7 app within a non-async method like ngOnInit()?

Despite the numerous questions and solutions available on Stack Overflow, none of them seem to work when called from a component's init function that is not asynchronous. Here's an example: private delay(ms: number) { return new Promise( ...

item uploaded via internet not restricted

Recently delving into Angular2, I am currently engrossed in constructing a basic application that showcases a list of objects. My goal is to click on one object and delve into a detailed view of it. However, while attempting to access the properties of the ...

Updating an element within a JSON object in PHP: A step-by-step guide

I have a JSON object that looks like this: { "fields" : [ { "name" : "news_title", "type" : "text", "value" : "old title" }, { "name" : "news_co ...

The Ultimate Guide for Formatting JSON Data from Firebase

I'm facing an issue with parsing JSON data returned by Firebase. Here is the JSON snippet: { "-JxJZRHk8_azx0aG0WDk": { "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cda6a68daaa0aca4a1e3aea2a0">[email&# ...

Building a TypeScript Rest API with efficient routing, controllers, and classes for seamless management

I have been working on transitioning a Node project to TypeScript using Express and CoreModel. In my original setup, the structure looked like this: to manage users accountRouter <- accountController <- User (Class) <- CoreModel (parent Class o ...

Error: Expected an expression but found a parsing error in the eslint code

interface Address { street: string, } export const getAddress = (address: Address | null) : string => address?.street ? `${address?.street}` : '0000 Default Dr'; Why am I receiving the error message Parsing error: Expression expected ...

Property of object (TS) cannot be accessed

My question relates to a piece of TypeScript code Here is the code snippet: export function load_form_actions() { $('#step_2_form').on('ajax:before', function(data) { $('#step_2_submit_btn').hide(); $(&ap ...