What could be causing the radio button to not be checked when the true condition is met in Angular?

I am working on an angular7 application that includes a dropdown list with radio buttons for each item. However, I am facing an issue where the radio button is not checked by default on successful conditions. Below is the code snippet from my component.html

<ul class="status-table-ul" (click)="$event.stopPropagation()">
<li class="status-table-li" [ngClass]="[selectedIndex === i ? 'selected' : '' , filterOpt.label === 'All' ? 'all' : '']" *ngFor="let filterOpt of referralFilterOptions;let i = index" (click)="onFilterSelect(i,filterOpt.label)">
    <label class="container">{{filterOpt.label}}
        <input type="radio" [(ngModel)]="selectedFilterOption" name="radio" [value]="filterOpt.label" [checked]="i === selectedIndex">
        <span class="checkmark"></span>
    </label>
 </li>
</ul>

Furthermore, in my component.ts file, the code looks like this

selectedIndex : number = 0;


onFilterSelect(index: number, opt: string){
    this.selectedIndex = index;
    this.refLinkArr = opt === 'All' ? this.masterRefLinkArr : this.masterRefLinkArr.filter(item => item.link_status === opt)
    $("ul.status-table-ul").removeClass("show-menu"); 

The issue I am facing is that upon component load, the radio button of the first element should be checked. However, it remains unchecked on page load. }

Answer №1

Delete this :

[(ngModel)]="selectedFilterOption"

Using

[(ngModel)]="selectedFilterOption"
along with [checked]="i === selectedIndex" simultaneously will cause the checked state to be determined by [(ngModel)].

When the value of selectedFilterOption is 1, it will be checked; however, if it's 0, then it will be unchecked.

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

Unsubscribing from an observable within a route resolve class in Angular

Within the ngOnDestroy method, I take care to unsubscribe from an observable to prevent multiple executions of the code... ngOnInit() { this.sub = this.route.params.subscribe(params => { this.projectId = +params['id']; ...

Is it possible to create a QR Code using Ionic 5?

Is there a way to generate QR Codes in Ionic 5? I attempted it, but keep receiving an error stating that the qrcode element is not recognized. Here is my code: qrcode.html <ion-item> <ion-input type="text" placeholder="My QR d ...

What is the best way to search for specific data in a MongoDB database?

When using angular2 in conjunction with meteor, the data contains the following information: { "_id" : "DxEraKtfYavoukdCK", "name" : "Aaron", "capacity" : 20, "available_capacity" : 15, "location" : "1" } { "_id" : "yMhEggaGmS7iio9P4", "name" : "Benard ...

Applying TPS rate to existing API endpoints at the method level within API Gateway using AWS CDK

I have successfully set up a UsagePlan and connected it to an API stage with CDK. However, I am having difficulty implementing method throttling at the API stage for a specific resource path. Despite searching online, I have not been able to find a satis ...

Encountered difficulty locating the module path 'stream/promises'

When importing the following in a typescript nodejs app import { pipeline } from "stream/promises"; Visual Studio Code (vscode) / eslint is showing an error message Unable to resolve path to module 'stream/promises' This issue appeare ...

Updating the variable in Angular 6 does not cause the view to refresh

I am facing an issue with my array variable that contains objects. Here is an example of how it looks: [{name: 'Name 1', price: '10$'}, {name: 'Name 2', price: '20$'}, ...] In my view, I have a list of products bei ...

How to retrieve the value of a selected item in primeng using p-dropdown and angular

Encountering an issue when trying to send the selected item value while iterating over an array of strings like: "BMW", "FERRARI", "AUDI", "BENTLY" Here is the structure of my HTML: <p-dropdown optionLabel="type" [options]="cars" formControlName="name" ...

The HTTP post method is not consistently triggering

Currently, I am in the process of developing a logout feature for angular (with a spring backend). The logout action is triggered by sending an HTTP post request to /auth/logout, where the endpoint invalidates the auth-token and refresh-token HTTP-only coo ...

Input for Shared Component with routerLink

I'm looking to develop a shared component featuring a button that, upon clicking, redirects the user to a specified location. I want the consumer of the component to be able to define the route. How can I make this happen? My current idea is to set t ...

Can TypeScript be used to dynamically render elements with props?

After extensive research on SO and the wider web, I'm struggling to find a solution. I have devised two components, Link and Button. In short, these act as wrappers for <a> and <button> elements with additional features like chevrons on t ...

What is preventing NgClass from applying the CSS styles?

I'm currently facing an issue in Angular2 where I am trying to apply different styles using ngClass within an 'NgFor' loop, but for some reason, it's not working as expected. Apologies for any language errors. <div class='line ...

Adjust text size based on device orientation changes

I'm struggling to dynamically change the font size based on screen orientation and width. How can I adjust the font size when switching between landscape and portrait mode? I attempted using an event listener, but it's not updating the font size. ...

React | What could be preventing the defaultValue of the input from updating?

I am working with a stateful component that makes a CEP promise to fetch data from post offices. This data is retrieved when the Zip input contains 9 characters - 8 numbers and a '-' - and returns an object with the desired information. Below is ...

How to Retrieve a File Using Angular 2

Currently, I am trying to download a file in pdf format using Angular 2. For this purpose, I have incorporated FileSaver.js to facilitate the saving of the file as a pdf. (response) => { var mediaType = 'application/pdf'; let pdfConte ...

There seems to be a compatibility issue between Angular 16 and Bootstrap 5 styling

In my angular.json, I have defined my styles in the following way: "styles": [ { "input": "node_modules/bootstrap/dist/css/bootstrap.min.css", "bundleName": "ltrCSS" }, { "input": "node_mod ...

Should we utilize the component @Input as a parameter for the injected service constructor, or should we opt for the ServiceFactory

Within Angular 12 lies a simplified component structured as follows: @Component({ selector: 'app-list', templateUrl: './list.component.html', styleUrls: ['./list.component.less'] }) export class ListComponent implements ...

Assign a distinct color to a row depending on the row's characteristic

One of my tasks involves implementing row highlights with specific colors based on their attributes. For instance, when displaying employee records, I need to show all employees whose highest education column is not null in green. The current code snippet ...

Every checkbox has been selected based on the @input value

My component has an @Input that I want to use to create an input and checkbox. import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'; @Component({ selector: 'app-aside', templateUrl: './aside.component ...

What is the best way to connect a ref to a stateless component in React?

I need help creating a stateless component with an input element that can be validated by the parent component. In my code snippet below, I'm facing an issue where the input ref is not being assigned to the parent's private _emailAddress propert ...

What are the different ways to customize the appearance of embedded Power BI reports?

Recently, I developed a website that integrates PowerBI embedded features. For the mobile version of the site, I am working on adjusting the layout to center the reports with a margin-left style. Below are the configuration parameters I have set up: set ...