43

I'm trying to mark as a default a radiobutton depending on the value I get from my object, it can be True or False. What could I do to mark as a default radiobutton depending on the option?

<label>This rule is true if:</label>
<label class="form-check-inline">
    <input class="form-check-input" type="radio" name="mode" value="true" 
        [(ngModel)]="rule.mode"> all of the following conditions are true
</label>
<label class="form-check-inline">
    <input class="form-check-input" type="radio" name="mode" value="false"
        [(ngModel)]="rule.mode"> at least one of the following conditions is true
</label>

I have the true or false set in rule.mode.

Marcos Dimitrio
  • 6,651
  • 5
  • 38
  • 62
Jotan
  • 607
  • 1
  • 5
  • 13

5 Answers5

92

You can use [(ngModel)], but you'll need to update your value to [value] otherwise the value is evaluating as a string. It would look like this:

<label>This rule is true if:</label>
<label class="form-check-inline">
    <input class="form-check-input" type="radio" name="mode" [value]="true" [(ngModel)]="rule.mode">
</label>
<label class="form-check-inline">
    <input class="form-check-input" type="radio" name="mode" [value]="false" [(ngModel)]="rule.mode">
</label>

If rule.mode is true, then that radio is selected. If it's false, then the other.

The difference really comes down to the value. value="true" really evaluates to the string 'true', whereas [value]="true" evaluates to the boolean true.

Tyler Jennings
  • 8,761
  • 2
  • 44
  • 39
  • In case, you still want the value to use 'true' as string and checked by default, you will need to set the value of rule.mode as true as default value. – Ajitesh May 24 '18 at 17:22
  • 1
    It looks like you're using ngModel on the same form field as formControlName. Support for using the ngModel input property and ngModelChange event with reactive form directives has been deprecated in Angular v6 and will be removed in Angular v7. For more information on this, see our API docs here: https://angular.io/api/forms/FormControlName#use-with-ngmodel – L. Kvri Jun 07 '18 at 20:58
  • 1
    Thanks for the answer and explanation. I was doing the same thing only the property for my radio button was type number. Without property binding on value it was being evaluated as a string instead of a number and the default radio button wasn't being checked. – Rich Aug 10 '18 at 00:05
7

We can use [(ngModel)] in following way and have a value selection variable radioSelected

Example tutorial

Demo Link

app.component.html

  <div class="text-center mt-5">
  <h4>Selected value is {{radioSel.name}}</h4>

  <div>
    <ul class="list-group">
          <li class="list-group-item"  *ngFor="let item of itemsList">
            <input type="radio" [(ngModel)]="radioSelected" name="list_name" value="{{item.value}}" (change)="onItemChange(item)"/> 
            {{item.name}}

          </li>
    </ul>
  </div>


  <h5>{{radioSelectedString}}</h5>

  </div>

app.component.ts

  import {Item} from '../app/item';
  import {ITEMS} from '../app/mock-data';

  @Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
  })
  export class AppComponent {
    title = 'app';
    radioSel:any;
    radioSelected:string;
    radioSelectedString:string;
    itemsList: Item[] = ITEMS;


      constructor() {
        this.itemsList = ITEMS;
        //Selecting Default Radio item here
        this.radioSelected = "item_3";
        this.getSelecteditem();
      }

      // Get row item from array  
      getSelecteditem(){
        this.radioSel = ITEMS.find(Item => Item.value === this.radioSelected);
        this.radioSelectedString = JSON.stringify(this.radioSel);
      }
      // Radio Change Event
      onItemChange(item){
        this.getSelecteditem();
      }

  }

Sample Data for Listing

        export const ITEMS: Item[] = [
            {
                name:'Item 1',
                value:'item_1'
            },
            {
                name:'Item 2',
                value:'item_2'
            },
            {
                name:'Item 3',
                value:'item_3'
            },
            {
                name:'Item 4',
                value:'item_4'
                },
                {
                    name:'Item 5',
                    value:'item_5'
                }
        ];
Code Spy
  • 9,626
  • 4
  • 66
  • 46
1

if you're using reactive forms then you can use the following way. consider the following example.

in component.html

 `<p class="mr-3"> Require Shipping: 

          <input type="radio" class="ml-2" value="true" name="requiresShipping" 
           id="requiresShipping" formControlName="requiresShipping">

                   &nbsp;  Yes  &nbsp;

          <input type="radio" class="ml-2" value="false" name="requiresShipping" 
          id="requiresShipping" formControlName="requiresShipping">

                   &nbsp;  No   &nbsp;
 </p>`

in component.ts

`
 export class ClassName implements OnInit {
      public yourForm: FormGroup
      
      constructor(
            private fromBuilder: FormBuilder
      ) {
            this.yourForm= this.fromBuilder.group({
                  requiresShipping: this.fromBuilder.control('true'),
            })
        }
 }

`

now you will get the default selected radio button.

enter image description here

AMAL MOHAN N
  • 1,416
  • 2
  • 14
  • 26
1

with Angular 12, here is the item worked for me

<label class="..-radio-box">
<input type="radio" name="radio" (click)="clickPayOption(false)"  [checked]="!isPayNow"  />
   Pay At Store
</label>
<label class="-radio-box">
<input type="radio" name="radio"  (click)="clickPayOption(true)"  [checked]="isPayNow" />
  Pay Now
</label>
In backend code
clickPayOption(selectedChoice:boolean) {
    this.isPayNow = selectedChoice;
  }
Anish Kutti
  • 337
  • 2
  • 7
0

This Default passing funtion will not work in normal javascripts. This Below method you can check with angular typescripts model value. -> Get the customer_size value from the api object name and you can compare with this method.

var customer_size = '3 BHK';
.wrapper{
    display: inline-flex;
    align-items: center;
    justify-content: space-evenly;
    width: 100%;
    height: 40px;
  }
  .wrapper .option{
    background: #fff;
    height: 100%;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-evenly;
    border-radius: 2px;
    cursor: pointer;
    border: 2px solid lightgrey;
    transition: all 0.3s ease;
    margin-right: 4px;
  }
  .wrapper .option .dot::before{
    position: absolute;
    content: "";
    top: 4px;
    left: 4px;
    width: 12px;
    height: 12px;
    background: #0069d9;
    border-radius: 50%;
    opacity: 0;
    transform: scale(1.5);
    transition: all 0.3s ease;
  }
  input[type="checkbox"]{
    display: none;
  }
  input[type="radio"]{
    display: none;
  }

  #option-1:checked:checked ~ .option-1,
  #option-2:checked:checked ~ .option-2,
  #option-3:checked:checked ~ .option-3,
  #option-4:checked:checked ~ .option-4,
  #option-5:checked:checked ~ .option-5,
  #option-6:checked:checked ~ .option-6,
  #option-7:checked:checked ~ .option-7,
  #option-8:checked:checked ~ .option-8,
  #option-9:checked:checked ~ .option-9,
  #option-10:checked:checked ~ .option-10,
  #option-11:checked:checked ~ .option-11,
  #option-12:checked:checked ~ .option-12,
  #option-13:checked:checked ~ .option-13,
  #option-14:checked:checked ~ .option-14,
  #option-15:checked:checked ~ .option-15,
  #option-16:checked:checked ~ .option-16,
  #option-17:checked:checked ~ .option-17,
  #option-18:checked:checked ~ .option-18
  {
    border-color: #e5652e;
    background: #e5652e;
  }
  .wrapper .option span{
    font-size: 14px;
    color: #808080;
  }
  #option-1:checked:checked ~ .option-1 span,
  #option-2:checked:checked ~ .option-2 span,
  #option-3:checked:checked ~ .option-3 span,
  #option-4:checked:checked ~ .option-4 span,
  #option-5:checked:checked ~ .option-5 span,
  #option-6:checked:checked ~ .option-6 span,
  #option-7:checked:checked ~ .option-7 span,
  #option-8:checked:checked ~ .option-8 span,
  #option-9:checked:checked ~ .option-9 span,
  #option-10:checked:checked ~ .option-10 span,
  #option-11:checked:checked ~ .option-11 span,
  #option-12:checked:checked ~ .option-12 span,
  #option-13:checked:checked ~ .option-13 span,
  #option-14:checked:checked ~ .option-14 span,
  #option-15:checked:checked ~ .option-15 span,
  #option-16:checked:checked ~ .option-16 span,
  #option-17:checked:checked ~ .option-17 span,
  #option-18:checked:checked ~ .option-18 span
  {
    color: #fff;
  }
<div class="wrapper">
                          <input type="radio" class="radiocheck" name="select3" value="1 BHK" id="option-7" [checked]="customer_size == '1 BHK'" >
                          <input type="radio" class="radiocheck" name="select3" value="2 BHK" id="option-8" [checked]="customer_size == '2 BHK'" >
                          <input type="radio" class="radiocheck" name="select3" value="3 BHK" id="option-9" [checked]="customer_size == '3 BHK'" >
                          <input type="radio" class="radiocheck" name="select3" value="4 BHK" id="option-10" [checked]="customer_size == '4 BHK'" >
                          <input type="radio" class="radiocheck" name="select3" value="5 BHK" id="option-11" [checked]="customer_size == '5 BHK'" >
                          <label for="option-7" class="option option-7">
                            <span>1 BHK</span>
                         </label>
                            <label for="option-8" class="option option-8">
                               <span>2 BHK</span>
                            </label>
                            <label for="option-9" class="option option-9">
                               <span>3 BHK</span>
                            </label>
                            <label for="option-10" class="option option-10">
                              <span>4 BHK</span>
                           </label>
                           <label for="option-11" class="option option-11">
                            <span>5 BHK</span>
                         </label>
                         </div>
Abijith Ajayan
  • 238
  • 3
  • 17