Angular 라디오 및 라디오 그룹 구성 요소 개요

    Radio Button

    Ignite UI for Angular 하면 사용자는 나란히 나열된 사용 가능한 옵션 세트에서 단일 옵션을 선택할 수 있습니다.

    Angular Radio & Radio Group Example

    Getting Started with Ignite UI for Angular Radio Button

    Ignite UI for Angular Radio Button 구성 요소를 시작하려면 먼저 Ignite UI for Angular 설치해야 합니다. 기존 Angular 애플리케이션에서 다음 명령을 입력합니다.

    ng add igniteui-angular
    

    Ignite UI for Angular에 대한 전체 소개를 보려면 시작하기 항목을 읽어보세요.

    다음 단계는 IgxRadioModule에서 app.module.ts 파일.

    // app.module.ts
    
    ...
    import { IgxRadioModule } from 'igniteui-angular';
    // import { IgxRadioModule } from '@infragistics/igniteui-angular'; for licensed package
    
    @NgModule({
        ...
        imports: [..., IgxRadioModule],
        ...
    })
    export class AppModule {
        public selected: any;
    }
    

    또는 16.0.0부터 IgxRadioGroupDirectiveIgxRadioComponent 독립 실행형 종속성으로 가져오거나 IGX_RADIO_GROUP_DIRECTIVES 토큰을 사용하여 구성 요소와 모든 지원 구성 요소 및 지시문을 가져올 수 있습니다.

    // home.component.ts
    
    import { FormsModule } from '@angular/forms';
    import { IGX_RADIO_GROUP_DIRECTIVES } from 'igniteui-angular';
    // import { IGX_RADIO_GROUP_DIRECTIVES } from '@infragistics/igniteui-angular'; for licensed package
    
    @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]
        /* or imports: [IgxRadioComponent, IgxRadioGroupDirective, FormsModule] */
    })
    export class HomeComponent {
        public selected: any;
    }
    

    이제 Ignite UI for Angular 가져왔으므로 igx-radio-group 지시문 및 igx-radio 구성 요소 사용을 시작할 수 있습니다.

    Using the Angular Radio Button

    구성요소 템플릿 내에서 다음 코드를 사용하여 라디오 버튼을 표시할 수 있습니다.

    <igx-radio [(ngModel)]="selected" value="option1">Option 1</igx-radio>
    <igx-radio [(ngModel)]="selected" value="option2">Option 2</igx-radio>
    

    Label

    labelPosition 속성은 라디오 구성 요소에서 레이블의 기본 위치를 변경하는 데 사용할 수 있습니다. 사용자는 beforeafter 중에서 선택할 수 있습니다. 지정하지 않으면 레이블은 라디오 버튼 뒤에 배치됩니다.

    <igx-radio [(ngModel)]="selected" value="option1" labelPosition="before">Option 1</igx-radio>
    <igx-radio [(ngModel)]="selected" value="option2" labelPosition="before">Option 2</igx-radio>
    

    Properties

    각각 특정 색상을 배경으로 적용하는 4개의 라디오 버튼을 추가하여 이전 샘플을 향상해 보겠습니다. div 요소의 backgroundColor 속성을 구성 요소의 selectedColor 속성에 바인딩합니다. selectedColor도 NgModel 지시어를 통해 양방향 바인딩 관계에 참여하므로 사용자가 다른 라디오 버튼(색상)을 선택할 때마다 해당 값이 업데이트됩니다.

    // radiogroup.component.ts
    ...
    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;
    
    <!--radiogroup.component.html-->
    <igx-radio *ngFor="let color of colors" 
               name="color" 
               [value]="color.hex" 
               [(ngModel)]="selectedColor">
               {{color.name}}
    </igx-radio>
    
    <div [style.background-color]="selectedColor">
        ...
    </div>
    

    양방향 데이터 바인딩에서 NgModel 지시문을 사용하지 않는 경우 FormsModule 가져와 NgModule의 가져오기 목록에 추가해야 합니다.

    최종 결과는 다음과 같습니다.

    스타일링

    라디오 버튼 스타일링을 시작하려면 모든 테마 기능과 구성 요소 믹스인이 있는 index 파일을 가져와야 합니다.

    @use "igniteui-angular/theming" as *;
    
    // IMPORTANT: Prior to Ignite UI for Angular version 13 use:
    // @import '~igniteui-angular/lib/core/styles/themes/index';
    

    가장 간단한 접근 방식에 따라 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
    );
    

    Using CSS variables

    마지막 단계는 애플리케이션에 사용자 정의 라디오 테마를 전달하는 것입니다.

    @include css-vars($custom-radio-theme);
    

    Using Theme Overrides

    Internet Explorer 11과 같은 이전 브라우저의 구성 요소에 스타일을 지정하려면 CSS 변수를 지원하지 않기 때문에 다른 접근 방식을 사용해야 합니다.

    구성 요소가 Emulated ViewEncapsulation을 사용하는 경우::ng-deep 사용하여 이 캡슐화를 penetrate 해야 합니다. 사용자 정의 테마가 다른 구성 요소에 유출되는 것을 방지하려면::ng-deep 앞에:host 선택기를 포함해야 합니다.

    :host {
         ::ng-deep {
            @include radio($custom-radio-theme);
        }
    }
    

    Radio Group

    Ignite UI for Angular 하위 라디오 구성 요소를 더 효과적으로 제어할 수 있고 템플릿 기반 및 반응형 양식을 지원하는 그룹화 컨테이너를 제공합니다.

    Demo

    Usage

    Radio Group 지시문은 다음과 같이 내보내집니다. NgModule, 따라서 애플리케이션에서 해야 할 일은 IgxRadioModule에서 app.module.ts 파일:

    // app.module.ts
    ...
    import { IgxRadioModule } from 'igniteui-angular';
    // import { IgxRadioModule } from '@infragistics/igniteui-angular'; for licensed package
    
    @NgModule({
        ...
        imports: [..., IgxRadioModule],
        ...
    })
    

    시작하려면 igxRadioGroup 생성하고 여러 igxRadio 구성 요소를 추가하세요.

    라디오 그룹에 대한 name 속성 설정은 필수 입니다.

    <!--radio-group.component.html-->
    <igx-radio-group name="fruitsRadioGroup">
        <igx-radio *ngFor="let fruit of fruits" value="{{fruit}}">
            {{fruit}}
        </igx-radio>
    </igx-radio-group>
    
    // radio-group.component.ts
    public fruits = ["Apple", "Mango", "Banana", "Orange"];
    

    Alignment

    사용 alignment 방향을 변경하는 입력 속성 igxRadio 라디오 그룹의 구성 요소. 사용자는 다음 중에서 선택할 수 있습니다. horizontal 그리고 vertical. 기본적으로 라디오 그룹 정렬은 수평입니다.

    //sample.component.ts
    import { RadioGroupAlignment } from "igniteui-angular";
    ...
    public alignment = RadioGroupAlignment.vertical;
    ...
    
    <!-- sample.component.html -->
    <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>
    

    API References

    Theming Dependencies

    Additional Resources

    우리 커뮤니티는 활동적이며 항상 새로운 아이디어를 환영합니다.