Displaying the ngFor data in the HTML section

Struggling with passing an array from poll-vote.component.ts to poll-vote.component.html. The data involves radio buttons and I'm using ngFor loop with index, but it's not working as expected:

Here is my poll-vote.component.ts code:

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-poll-vote',
  templateUrl: './poll-vote.component.html',
  styleUrls: ['./poll-vote.component.scss'],
})
export class PollVoteComponent implements OnInit {
  pollvoteForm: FormGroup;
  Options: ['Monday', 'Tuesday', 'Wednesday'];
  constructor(private fb: FormBuilder) {
    this.pollvoteForm = this.fb.group({
      selection: this.fb.control('', [Validators.required]),
    });
  }

  ngOnInit(): void {}

  submitForm() {
    console.log(this.pollvoteForm.value);
  }
}

This is the code snippet for poll-vote.component.html:

<h1>Poll Question title</h1>
<form [formGroup]="pollvoteForm" (ngSubmit)="submitForm()">
  <div class="form-check" *ngFor="let opt of Options; let i=index">
    <h1>{{opt}}</h1>
    <input
      type="radio"
      class="form-check-input"
      formControlName="selection"
      [value]="i"
    />
    <label class="form-check-label"> {{opt}} </label>
  </div>
  <button type="submit" class="btn btn-secondary mt-3">Submit Votes</button>
</form>

Being new to Angular, I'm still learning and navigating through the challenges. Any help would be greatly appreciated.

Answer №1

At last, the puzzle has been unraveled. It turned out to be a simple oversight on my part - I had neglected to declare the array properly. Here is the corrected code snippet:

DaysOfWeek:Array<string>= ['Monday','Tuesday','Wednesday'];

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

Issue with Angular detection on the webpage: Protractor and headless chrome unable to locate Angular

Running end-to-end tests on my Angular2 app using Chrome works perfectly fine. However, when attempting to use headless Chrome with additional chromeOptions, it fails to locate the Angular app. I have tried setting directConnect to true and starting the se ...

What is the best approach to incorporate a refresh feature for a data stream?

I need to implement a function that updates the current list of items$ and allows for awaiting refreshItems(). This is my working implementation: private readStream = new Subject<T[]>(); readStream$ = this.readStream.asObservable(); getItems = (): ...

An error occurred: Unable to locate the file or assembly 'Interop.iTunesLib, Version=1.13.0.0, Culture=neutral, PublicKeyToken=null'

I've been attempting to connect to iTunes using C# programming language. The process involves creating a dll in C# and running it with TypeScript through the Overwolf API. Here's what I've done so far: Generated a .dll file I utilized the ...

Angular Formly allows for the creation of custom field types

I've been struggling to find a solution for several days now, but without any success. The issue arises when I try to use Angular Formly, Angular Material, and Angular 17 to create mat-select as a custom type. Whenever I attempt to utilize the mat-sel ...

Utilize the useState() hook to add an object and display its data in a React Native

Here is a function I am working with: const [dataLoc, setDataLoc] = useState({date: "No data received yet from sensor", coords: {}}); This is where I set the location: Geolocation.getCurrentPosition( location => { const date = d ...

"NameService is not provided in Angular, please check your module

I've been struggling with loading a class into my Angular component. I've spent quite some time trying to figure out the solution, even attempting to consolidate everything into a single file. Here is what I have: Application.ts /// <referenc ...

TS2307 Error: The specified module (external, private module) could not be located

I have come across similar queries, such as tsc throws `TS2307: Cannot find module` for a local file . In my case, I am dealing with a private external module hosted on a local git server and successfully including it in my application. PhpStorm is able ...

Monitor the input value for any changes in Angular 8 using the listen component

Hey there! I'm currently working with a component that includes the input @Input() userId: number[] = []; to receive a list of user IDs. Specifically, I have integrated this component into another one, such as the news component: <kt-user-post-li ...

Generate md-card components in real-time using data fetched from an API

I have a scenario where I make an API call to fetch user profiles, and I want to generate Angular Material Design md-cards dynamically for each profile. The number of profiles retrieved can vary, hence the need for dynamic card creation. Below is the comp ...

The compatibility of return value types between the constructor signature and the call signature interface is not maintained when they are used together

I'm new to TypeScript and I'm struggling to figure out why I'm getting a type error in the code below. Can someone help me identify what's wrong? interface CallOrConstruct { new (value: string): Person (value: number): number } cla ...

Is it possible to receive notifications when the value of a standalone input changes by subscribing to its value

I'm curious about how to subscribe to the valueChanges property of a standalone form field within a parent form. The example code below shows a field without a formcontrolName: <mat-form-field> <mat-label for="userName">Name< ...

Is there a way to extract information from an HttpClient Rest Api through interpolation?

I am currently facing an issue with a component in my project. The component is responsible for fetching data from a REST API using the HttpClient, and the data retrieval seems to be working fine as I can see the data being logged in the Console. However, ...

I am attempting to update the URL of an iframe dynamically, but I am encountering an issue: the Error message stating that an Unsafe value is being

Currently, I am attempting to dynamically alter the src of an iframe using Angular 2. Here is what I have tried: HTML <iframe class="controls" width="580" height="780" src="{{controllerSrc}}" frameborder="0" allowfullscreen></iframe> COMPONE ...

The feature module does not have visibility of the Behavioral Subject Value during lazy loading

I am currently working on integrating Shopping Cart Functionality using Angular. Products can be added to the shopping cart either through the dedicated Shopping Cart Page or by clicking the 'ADD TO CART' button on the Product Details page (both ...

Creating a JAR file for an Angular 2 application with Maven

My Front-End application is constructed using Angular2 + Webpack, organized as follows: > app > config > dist > dll > node_modules > src pom.xml package.json ... (other files) Currently, I am exploring th ...

What advantages does utilizing Angular services for API calls provide?

What purpose does the Angular service serve when making HTTP requests or API calls to the backend, and what are the advantages of using it? ...

What is the recommended module to choose when importing MAT_DIALOG_DATA?

When working on my Angular project, I noticed that there are multiple options available for importing MAT_DIALOG_DATA. I have tried importing both @angular/material and @angular/material/dialog, and found that both options work successfully. However, I a ...

The flexbox layout is not properly stacking div rows in a column

Objective: Creating a flexbox layout with a responsive background image that adjusts in height when the screen size changes, causing elements to wrap; In addition, there is a fixed header bar at the top of the page. The layout consists of a full-screen co ...

Modifying a property in a nested layout through a page in Next.js 13

Currently, I am facing a challenge in updating the page title within a nested layout structure. app/docs/layout.tsx: export const DocsLayout = ({children}:{children: React.ReactNode}) => { return ( <> <header> ...

Utilizing Radio buttons to establish default values - a step-by-step guide

I am utilizing a Map to store the current state of my component. This component consists of three groups, each containing radio buttons. To initialize default values, I have created an array: const defaultOptions = [ { label: "Mark", value: & ...