41

I'm working on a small reusable Component which styles radio buttons and emits the selected values.

import { Component, OnInit, Input, Output, EventEmitter } from "@angular/core";

@Component({
    moduleId: module.id,
    selector: 'button-select',
    template: `<div class="toggle-group">
                    <div *ngFor="let choice of choices">
                        <input type="radio"
                               id="{{ groupName + choice }}"
                               name="{{groupName}}"
                               value="{{ choice }}"
                               [checked]="choice === defaultChoice"
                               [(ngModel)]="value"
                               (ngModelChange)="choose($event)" />
                        <label class="toggle-button"
                               for="{{ groupName + choice }}">{{ choice }}</label>
                    </div>
                </div>`,
    styleUrls: [
        'editableField.css',
        'buttonSelect.css'
    ]
})

export class ButtonSelectComponent implements OnInit {
    @Input() choices: string[];
    @Input() defaultChoice: string;
    @Input() groupName: string;
    @Input() value: string;

    @Output() valueChosen: EventEmitter<any> = new EventEmitter();

    ngOnInit() {
        this.choose(this.defaultChoice);
    }

    private choose(value: string) {
        this.valueChosen.emit(value);
    }
}

The component is implemented like so:

<button-select #statusFilter
               [choices]="['All', 'Active', 'Draft']"
               [defaultChoice]="'All'"
               [groupName]="'statusFilter'"
               (valueChosen)="filterChosen('statusFilter', $event)"
</button-select>

Before adding [(ngModel)]="value" (ngModelChange)="choose($event)" to the button-select Component, the [checked]="choice === defaultChoice" directive correctly set the checked attribute on the relevant <input />.

After adding the [(ngModel)], only ng-reflect-checked="true" gets set, which prevents the visual styling from showing the default value (since my CSS uses a pseudo-selector).

Changing [(ngModel)] for [ngModel] had no effect.

Why did this happen and how can I fix it?

1
  • 1
    Short notice to the name attribute which you used: name="{{groupName}}" won't encapsulate your radio-group. You will be able to navigate to another radio-button in another radio-group on the same page using your arrow-keys once you tab to any radio-group. The reason is that Angular won't add the name attribute itself but ng-reflect-name.
    – Ilker Cat
    Nov 27, 2017 at 17:28

3 Answers 3

65

I think, you don't need this [checked]="choice === defaultChoice". Try this :

<input type="radio"
       id="{{ groupName + choice }}"
       name="{{groupName}}"
       [value]="choice"
       [(ngModel)]="defaultChoice"
       (ngModelChange)="choose($event)" />

When [value] = [(ngModel)] the radio is selected.

2
  • This is exactly correct! I had added [checked] early on before thinking to add the [(ngModel)] and never questioned it. Thank you.
    – msanford
    Feb 24, 2017 at 20:00
  • @mickdev and @msanford i have exact same problem that my psudeo css (input:checked) is not applied. the surprising fact is the exact same code is working in other angular project. this stackblitz is also working, which is replica, but i am not sure why my project do not show the 3 hightlights, when i inspect at both page (from different project) i see that ng-reflect-model is not added in my buggy project. please help
    – GKhedekar
    May 18, 2022 at 5:18
4

I was able to emit the value and retain the default styling with minimal changes by altering the input's template to:

<input type="radio"
       id="{{ groupName + choice }}"
       name="{{groupName}}"
       value="{{ choice }}"
       [checked]="choice === defaultChoice"
       (click)="choose($event['target']['value'])" />

...which I find kind of hacky. It also doesn't explain why adding data/property binding broke it, so I'm open to more suggestions.

1
  • Using click to handle radiobuttons is a pretty bad suggestion. At least because clicking is not the only way to switch radiobutton. One can also do it with keyboard. Better use "change" event if you don't want ngModel for some reason. Jul 12, 2021 at 12:50
3

export class ConfirmationmodalComponent implements OnInit {
  client_notification: any = false;
  candidate_notification: any = false;
  cancel_associated_session: any = false;

  constructor(
  ) {}

  ngOnInit(): void {
  }
}
<div class="form-check form-check-inline">
  <input
    class="form-check-input"
    type="radio"
    id="inlineRadio1"
    name="cancel_associated_session"
    [(ngModel)]="cancel_associated_session"
    [value]="true"
  />
  <label class="form-check-label" for="inlineRadio1">
    Yes
  </label>
</div>
<div class="form-check form-check-inline">
  <input
    class="form-check-input"
    type="radio"
    id="inlineRadio2"
    name="cancel_associated_session"
    [(ngModel)]="cancel_associated_session"
    [value]="false"
  />
  <label class="form-check-label" for="inlineRadio2">
    No
  </label>
</div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.