Компоненты PrimeNG
RadioButton

Радиокнопка

PrimeNG/RadioButton (opens in a new tab)

Радиокнопка - это расширение стандартной радиокнопки с тематизацией.

С чего начать

Подключите модуль

import { RadioButtonModule } from 'primeng/radiobutton';

В .ts файле

import { Component, OnInit } from '@angular/core';
 
@Component({
  selector: 'app-prime-radiobutton',
  templateUrl: './prime-radiobutton.component.html',
  styleUrls: ['./prime-radiobutton.component.scss']
})
export class PrimeRadiobuttonComponent implements OnInit {
 
  constructor() { }
 
  city: string;
  selectedCategory: any = null;
  categories: any[] = [{name: 'Accounting', key: 'A'}, {name: 'Marketing', key: 'M'}, {name: 'Production', key: 'P'}, {name: 'Research', key: 'R'}];
 
  ngOnInit(): void {
    this.selectedCategory = this.categories[1];
  }
}

Встройте компонент с помощью тэга p-radioButton.

<p-radioButton name="city" value="Chicago" [(ngModel)]="city" inputId="city1"></p-radioButton>

Basic

ChicagoLos AngelesNew YorkSan Francisco

<div class="p-field-radiobutton">
  <p-radioButton name="city" value="Chicago" [(ngModel)]="city" inputId="city1"></p-radioButton>
  <label for="city1">Chicago</label>
</div>
<div class="p-field-radiobutton">
  <p-radioButton name="city" value="Los Angeles" [(ngModel)]="city" inputId="city2"></p-radioButton>
  <label for="city2">Los Angeles</label>
</div>
<div class="p-field-radiobutton">
  <p-radioButton name="city" value="New York" [(ngModel)]="city" inputId="city3"></p-radioButton>
  <label for="city3">New York</label>
</div>
<div class="p-field-radiobutton">
  <p-radioButton name="city" value="San Francisco" [(ngModel)]="city" inputId="city4"></p-radioButton>
  <label for="city4">San Francisco</label>
</div>

Dynamic Values, Preselection, Value Binding and Disabled Option

category.name

<div *ngFor="let category of categories" class="p-field-checkbox">
  <p-radioButton [inputId]="category.key" name="category" [value]="category" [(ngModel)]="selectedCategory" [disabled]="category.key === 'R'"></p-radioButton>
  <label [for]="category.key"> { { category.name } } </label>
</div>

Свойства

NameTypeDefaultDescription
namestringnullName of the radiobutton group.
valueanynullValue of the radiobutton.
labelstringnullLabel of the radiobutton.
disabledbooleanfalseWhen present, it specifies that the element should be disabled.
tabindexnumbernullIndex of the element in tabbing order.
inputIdstringnullIdentifier of the focus input to match a label defined for the component.
ariaLabelledBystringnullEstablishes relationships between the component and label(s) where its value should be one or more element IDs.
ariaLabelstringnullUsed to define a string that labels the input element.
styleobjectnullInline style of the component.
styleClassstringnullStyle class of the component.
labelStyleClassstringnullStyle class of the label.

События

NameParametersDescription
onClickevent: Click eventCallback to invoke on radio button click.
onFocusevent: Focus eventCallback to invoke when the radio button receives focus.
onBlurevent: Blur eventCallback to invoke when the radio button loses focus.

Методы

NameParametersDescription
focus-Applies focus.

Стилизация

NameElement
p-radiobuttonContainer element
p-radiobutton-boxContainer of icon.
p-radiobutton-iconIcon element.
p-chkbox-labelLabel element.
p-label-activeLabel element of a checked state.
p-label-focusLabel element of a focused state.
p-label-disabledLabel element of a disabled state.