Search code examples
angulartypescriptradio-buttonradio-group

How can I use Angular to determine if a given value matches the value of a specific radio button in my application?


I have a list of Arrays. such.

enter image description here

I show this in ratio buttons. like this

enter image description here

enter image description here

I want to click the check box similar to this value in the data loaded from my other array

enter image description here

How to resolve this?

[(ngModel)]=" " , value="" ,[checked]="true",

I tried these 3.

But it didn't go well.


Solution

  • Here is a similar app that works. Compare with your code. I used [checked] attribute.

    import 'zone.js/dist/zone';
    import { Component } from '@angular/core';
    import { CommonModule } from '@angular/common';
    import { bootstrapApplication } from '@angular/platform-browser';
    
    @Component({
      selector: 'my-app',
      standalone: true,
      imports: [CommonModule],
      template: `
        <div *ngFor="let payMode of payModes">
          <input type="radio" name="pm" [value]="payMode.id" [checked]="selectedPayMode === payMode.id" />
          {{ payMode.name }}
        </div>
      `,
    })
    export class App {
      payModes = [
        {
          id: 1,
          name: 'Monthly'
        },
        {
          id: 2,
          name: 'Quarterly'
        },
        {
          id: 3,
          name: 'Yearly'
        }
      ];
      selectedPayMode = 2;
    }
    
    bootstrapApplication(App);