Angular Radio 및 Radio Group 구성 요소 개요
Ignite UI for Angular 사용하면 사용자는 나란히 나열된 사용 가능한 옵션 세트에서 단일 옵션을 선택할 수 있습니다.
Angular 라디오 & 라디오 그룹.
import { Component } from '@angular/core' ;
import { IgxRadioComponent } from 'igniteui-angular' ;
import { FormsModule } from '@angular/forms' ;
@Component ({
selector : 'app-radio-sample-1' ,
styleUrls : ['./radio-sample-1.component.scss' ],
templateUrl : './radio-sample-1.component.html' ,
imports : [IgxRadioComponent, FormsModule]
})
export class RadioSample1Component {
public selected: string ;
}
ts コピー <igx-radio [(ngModel )]="selected" value ="option1" > Option 1</igx-radio >
<igx-radio [(ngModel )]="selected" value ="option2" > Option 2</igx-radio >
html コピー :host {
display : flex;
flex-flow : column nowrap;
padding : 16px ;
}
igx-radio + igx-radio {
margin-top : 16px ;
}
scss コピー
이 샘플이 마음에 드시나요? 전체 Ignite UI for Angular 툴킷에 액세스하고 몇 분 안에 나만의 앱을 구축해 보세요. 무료로 다운로드하세요.
Ignite UI for Angular Radio Button 구성 요소를 시작하려면 먼저 Ignite UI for Angular 설치해야 합니다. 기존 Angular 애플리케이션에서 다음 명령을 입력합니다.
ng add igniteui-angular
cmd
Ignite UI for Angular에 대한 전체 소개는 시작 항목을 참조하십시오.
다음 단계는 IgxRadioModule
에서 app.module.ts 파일.
...
import { IgxRadioModule } from 'igniteui-angular' ;
@NgModule ({
...
imports : [..., IgxRadioModule],
...
})
export class AppModule {
public selected: any ;
}
typescript
또는 16.0.0
부터 IgxRadioGroupDirective
및 IgxRadioComponent
독립 실행형 종속성으로 가져오거나 IGX_RADIO_GROUP_DIRECTIVES
토큰을 사용하여 구성 요소와 모든 지원 구성 요소 및 지시문을 가져올 수 있습니다.
import { FormsModule } from '@angular/forms' ;
import { IGX_RADIO_GROUP_DIRECTIVES } from 'igniteui-angular' ;
@Component ({
selector : 'app-home' ,
template : `
<igx-radio-group>
<igx-radio [(ngModel)]="selected" value="London">London</igx-radio>
<igx-radio [(ngModel)]="selected" value="New York">New York</igx-radio>
<igx-radio [(ngModel)]="selected" value="Tokyo">Tokyo</igx-radio>
<igx-radio [(ngModel)]="selected" value="Sofia">Sofia</igx-radio>
</igx-radio-group>
` ,
styleUrls : ['home.component.scss' ],
standalone : true ,
imports : [IGX_RADIO_GROUP_DIRECTIVES, FormsModule],
})
export class HomeComponent {
public selected: any ;
}
typescript
이제 Ignite UI for Angular 가져왔으므로 igx-radio-group
지시문과 igx-radio
구성 요소를 사용할 수 있습니다.
구성요소 템플릿 내에서 다음 코드를 사용하여 라디오 버튼을 표시할 수 있습니다.
<igx-radio [(ngModel )]="selected" value ="option1" > Option 1</igx-radio >
<igx-radio [(ngModel )]="selected" value ="option2" > Option 2</igx-radio >
html
상표
labelPosition
속성은 라디오 구성 요소에서 레이블의 기본 위치를 변경하는 데 사용할 수 있습니다. 사용자는 before
과 after
중에서 선택할 수 있습니다. 지정하지 않으면 레이블은 라디오 버튼 뒤에 배치됩니다.
<igx-radio [(ngModel )]="selected" value ="option1" labelPosition ="before" > Option 1</igx-radio >
<igx-radio [(ngModel )]="selected" value ="option2" labelPosition ="before" > Option 2</igx-radio >
html
속성
각각 특정 색상을 배경으로 적용하는 4개의 라디오 버튼을 추가하여 이전 샘플을 향상해 보겠습니다. div 요소의 backgroundColor 속성을 구성 요소의 selectedColor 속성에 바인딩합니다. selectedColor도 NgModel
지시어를 통해 양방향 바인딩 관계에 참여하므로 사용자가 다른 라디오 버튼(색상)을 선택할 때마다 해당 값이 업데이트됩니다.
...
public colors = [{
hex : '#f06a2f' ,
name : 'Carrot'
}, {
hex : '#ff134a' ,
name : 'Watermelon'
}, {
hex : '#7bc96f' ,
name : 'Grass'
},
{
hex : 'transparent' ,
name : 'No color'
}];
public selectedColor: string = this .colors[3 ].hex;
typescript
<igx-radio *ngFor ="let color of colors" name ="color" [value ]="color.hex" [(ngModel )]="selectedColor" >
{{color.name}}
</igx-radio >
<div [style.background-color ]="selectedColor" > ...</div >
html
양방향 데이터 바인딩에서 NgModel
지시문을 사용하지 않는 경우 FormsModule
가져와 NgModule의 가져오기 목록에 추가해야 합니다.
최종 결과는 다음과 같습니다.
import { Component } from '@angular/core' ;
import { NgFor } from '@angular/common' ;
import { IgxRadioComponent } from 'igniteui-angular' ;
import { FormsModule } from '@angular/forms' ;
@Component ({
selector : 'app-radio-sample-2' ,
styleUrls : ['./radio-sample-2.component.scss' ],
templateUrl : './radio-sample-2.component.html' ,
imports : [NgFor, IgxRadioComponent, FormsModule]
})
export class RadioSample2Component {
public colors = [{
hex : '#f06a2f' ,
name : 'Carrot'
}, {
hex : '#ff134a' ,
name : 'Watermelon'
}, {
hex : '#7bc96f' ,
name : 'Grass'
},
{
hex : 'transparent' ,
name : 'No color'
}];
public selectedColor: string = this .colors[3 ].hex;
}
ts コピー <igx-radio *ngFor ="let color of colors" name ="color" [value ]="color.hex" [(ngModel )]="selectedColor" >
{{color.name}}
</igx-radio >
<div class ="box" [style.background-color ]="selectedColor" >
<div >
<h5 > New York City</h5 >
New York City comprises 5 boroughs sitting where the Hudson River meets the Atlantic Ocean. At its core is
Manhattan, a densely populated borough that's among the world's major commercial, financial and cultural
centers.
</div >
</div >
html コピー :host {
display : flex;
flex-flow : column nowrap;
padding : 16px ;
}
igx-radio + igx-radio {
margin-top : 16px ;
}
.box {
display : flex;
justify-content : center;
align-items : center;
margin-top : 24px ;
max-width : 400px ;
height : 250px ;
padding : 16px ;
text-align : center;
border-radius : 4px ;
box-shadow : 0px 2px 6px rgba(0 , 0 , 0 , .24 );
transition : background-color .2s ease-out;
}
h5 {
margin : 0 0 16px 0 ;
}
scss コピー
스타일링
라디오 버튼 스타일링을 시작하려면 모든 테마 기능과 구성 요소 믹스인이 있는 index
파일을 가져와야 합니다.
@use "igniteui-angular/theming" as *;
scss
가장 간단한 접근 방식에 따라 radio-theme
확장하고 기본 테마의 매개변수 중 일부를 허용하는 새 테마를 만듭니다.
$custom-radio-theme : radio-theme(
$disabled-color : lightgray,
$empty-color : #345779 ,
$fill-color : #2dabe8 ,
$fill-color-hover : #2dabe8 ,
$fill-hover-border-color : #2dabe8 ,
);
scss
마지막 단계는 애플리케이션에 사용자 정의 라디오 테마를 전달하는 것입니다.
@include css-vars($custom-radio-theme );
scss
import { Component } from '@angular/core' ;
import { IgxRadioComponent } from 'igniteui-angular' ;
import { FormsModule } from '@angular/forms' ;
@Component ({
selector : 'app-radio-sample-1' ,
styleUrls : ['./radio-styling-sample.component.scss' ],
templateUrl : './radio-styling-sample.component.html' ,
imports : [IgxRadioComponent, FormsModule]
})
export class RadioStylingSampleComponent {
public selected: string ;
}
ts コピー <div class ="radio-wrapper" >
<igx-radio [(ngModel )]="selected" value ="option1" > New York</igx-radio >
<igx-radio [(ngModel )]="selected" value ="option2" > London</igx-radio >
<igx-radio [(ngModel )]="selected" value ="option3" > Sofia</igx-radio >
<igx-radio [(ngModel )]="selected" value ="option4" > Tokyo</igx-radio >
<igx-radio [(ngModel )]="selected" value ="option5" [disabled ] = "true" > Singapore</igx-radio >
</div >
html コピー @use "layout.scss" ;
@use "igniteui-angular/theming" as *;
$custom-radio-theme : radio-theme(
$disabled-color : lightgray,
$empty-color : #345779 ,
$fill-color : #2dabe8 ,
$fill-color-hover : #2dabe8 ,
$fill-hover-border-color : #2dabe8
);
@include css-vars($custom-radio-theme );
scss コピー
라디오 그룹
Ignite UI for Angular 자식 라디오 구성 요소를 더 잘 제어할 수 있는 그룹화 컨테이너를 제공하고 템플릿 기반 및 반응형 양식을 지원합니다.
데모
import { Component } from '@angular/core' ;
import { FormBuilder, FormGroup, Validators, FormsModule, ReactiveFormsModule } from '@angular/forms' ;
import { IgxInputGroupComponent, IgxInputDirective, IgxRadioGroupDirective, IgxRadioComponent, IgxButtonDirective, IgxRippleDirective, IgxLabelDirective } from 'igniteui-angular' ;
import { NgFor, NgIf } from '@angular/common' ;
@Component ({
selector : 'app-radio-group-sample' ,
styleUrls : ['./radio-group-sample.component.scss' ],
templateUrl : './radio-group-sample.component.html' ,
imports : [FormsModule, ReactiveFormsModule, IgxInputGroupComponent, IgxInputDirective, IgxRadioGroupDirective, NgFor, IgxRadioComponent, IgxButtonDirective, IgxRippleDirective, NgIf, IgxLabelDirective]
})
export class RadioGroupSampleComponent {
public fruitsForm: FormGroup;
public fruits = ['Apple' , 'Mango' , 'Banana' , 'Orange' ];
public newModel: FruitData;
public model: FruitData;
constructor (private _formBuilder: FormBuilder ) {
this .model = {
favFruit : this .fruits[0 ],
fullName : 'John Doe'
};
this .createForm();
}
public onSubmit ( ) {
if (this .fruitsForm.valid) {
this .newModel = {
favFruit : this .fruitsForm.value.favoriteFruit,
fullName : this .fruitsForm.value.fullName
};
} else {
this .newModel = null ;
}
}
public onReset ( ) {
this .fruitsForm.patchValue({
favoriteFruit : this .model.favFruit,
fullName : this .model.fullName
});
this .newModel = null ;
}
private createForm ( ) {
this .fruitsForm = this ._formBuilder.group({
favoriteFruit : ['' , Validators.required],
fullName : ''
});
this .fruitsForm.setValue({
favoriteFruit : this .model.favFruit,
fullName : this .model.fullName
});
}
}
export class FruitData {
public fullName: string ;
public favFruit: string ;
}
ts コピー <div class ="sample-wrapper" >
<label > Choose a fruit and submit your choice:</label >
<form [formGroup ]="fruitsForm" (ngSubmit )="onSubmit()" >
<igx-input-group >
<input igxInput name ="fullName" type ="text" formControlName ="fullName" required />
</igx-input-group >
<igx-radio-group name ="fruitsRadioGroup" formControlName ="favoriteFruit" >
<igx-radio class ="radio-sample" *ngFor ="let fruit of fruits" value ="{{fruit}}" >
{{fruit}}
</igx-radio >
</igx-radio-group >
<button igxButton ="contained" igxRipple type ="submit" > Submit</button >
<button igxButton ="contained" igxRipple type ="button" (click )="onReset()" > Reset</button >
</form >
<label igxLabel *ngIf ="newModel" > {{newModel.fullName}} favourite fruit is {{newModel.favFruit}}.</label >
</div >
html コピー .sample-wrapper {
margin : 16px ;
}
form {
max-width : 380px ;
margin-bottom : 16px ;
}
igx-radio {
margin : 8px ;
}
.igx-button--contained {
margin-right : 16px ;
}
scss コピー
용법
Radio Group 지시문은 다음과 같이 내보내집니다. NgModule
, 따라서 애플리케이션에서 해야 할 일은 IgxRadioModule
에서 app.module.ts 파일:
...
import { IgxRadioModule } from 'igniteui-angular' ;
@NgModule ({
...
imports : [..., IgxRadioModule],
...
})
typescript
시작하려면 igxRadioGroup
생성하고 여러 igxRadio
구성 요소를 추가하세요.
라디오 그룹에 대한 name
속성 설정은 필수 입니다.
<igx-radio-group name ="fruitsRadioGroup" >
<igx-radio *ngFor ="let fruit of fruits" value ="{{fruit}}" >
{{fruit}}
</igx-radio >
</igx-radio-group >
html
public fruits = ["Apple" , "Mango" , "Banana" , "Orange" ];
typescript
조정
사용 alignment
방향을 변경하는 입력 속성 igxRadio
라디오 그룹의 구성 요소. 사용자는 다음 중에서 선택할 수 있습니다. horizontal
그리고 vertical
. 기본적으로 라디오 그룹 정렬은 수평입니다.
import { RadioGroupAlignment } from "igniteui-angular" ;
...
public alignment = RadioGroupAlignment.vertical;
...
typescript
<igx-radio-group [alignment ]="alignment" >
<igx-radio [(ngModel )]="selected" value ="London" > London</igx-radio >
<igx-radio [(ngModel )]="selected" value ="New York" > New York</igx-radio >
<igx-radio [(ngModel )]="selected" value ="Tokyo" > Tokyo</igx-radio >
<igx-radio [(ngModel )]="selected" value ="Sofia" > Sofia</igx-radio >
</igx-radio-group >
html
import { Component } from '@angular/core' ;
import { RadioGroupAlignment, IgxRadioGroupDirective, IgxRadioComponent } from 'igniteui-angular' ;
import { FormsModule } from '@angular/forms' ;
@Component ({
selector : 'app-radio-group-vertical' ,
styleUrls : ['./radio-group-vertical.component.scss' ],
templateUrl : './radio-group-vertical.component.html' ,
imports : [IgxRadioGroupDirective, IgxRadioComponent, FormsModule]
})
export class RadioGroupVerticalComponent {
public alignment = RadioGroupAlignment.vertical;
public selected: string ;
}
ts コピー <article class ="sample-column" >
<igx-radio-group [alignment ]="alignment" >
<igx-radio [(ngModel )]="selected" value ="London" > London</igx-radio >
<igx-radio [(ngModel )]="selected" value ="New York" > New York</igx-radio >
<igx-radio [(ngModel )]="selected" value ="Tokyo" > Tokyo</igx-radio >
<igx-radio [(ngModel )]="selected" value ="Sofia" > Sofia</igx-radio >
</igx-radio-group >
</article >
html コピー igx-radio {
margin-bottom : 8px ;
}
scss コピー
API 참조
테마 종속성
추가 리소스
우리 커뮤니티는 활동적이며 항상 새로운 아이디어를 환영합니다.