Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- angular route
- 앵귤러 모달
- prisma
- 스크롤 이벤트 감지
- angular button
- ApexChart
- summary
- 검색
- egov spring ajax 사용 예시
- Angular Router
- 셀렉트박스 커스텀
- mysql if
- ajax 사용 예시
- 모달
- scroll
- Ionic modal
- Oracle LISTAGG 사용 예시
- angular animation
- angular modal
- TAILWIND
- formgroup
- flex-1
- 아이오닉 스크롤 이벤트
- 앵귤러 애니메이션
- 옵저버블
- 스크롤 이벤트
- route
- modal
- 호버
- Router
Archives
- Today
- Total
깜놀하는 해므찌로
Angular Radio Component 생성 및 활용 예시 본문
반응형
SMALL
/* option */
// 템플릿
<label [for]="value" (click)="selected$.emit(value)">
<div class="flex gap-2 items-center cursor-pointer">
<div class="text-sm text-gray-500 flex">
<ng-content />
</div>
<input
class="cursor-pointer w-4 h-4 appearance-none ring-gray-300 bg-gray-300 rounded-full ring-2 ring-offset-2 indeterminate:ring-gray-300 checked:ring-primary checked:bg-primary transition-all"
type="radio"
[id]="value"
[name]="name"
[value]="value"
[checked]="checked"
[disabled]="disabled"
/>
</div>
</label>
// 컴포넌트
import { Component, EventEmitter, Input, Output } from '@angular/core';
@Component({
selector: 'app-radio-option',
templateUrl: './radio-option.component.html',
styleUrls: ['./radio-option.component.scss'],
standalone: true,
})
export class RadioOptionComponent {
@Output() selected$: EventEmitter<any> = new EventEmitter<any>();
@Input() value: any;
@Input() disabled: boolean = false;
@Input() name!: string;
@Input() checked: boolean = false;
}
/* radio */
// 템플릿
<div class="flex flex-col gap-3">
<span class="text-sm font-semibold text-gray-500" *ngIf="label">{{
label
}}</span>
<div class="flex gap-4 select-none">
<ng-content />
</div>
</div>
// 컴포넌트
import {
AfterContentInit,
Component,
ContentChildren,
Input,
Optional,
QueryList,
Self,
} from '@angular/core';
import { NgControl } from '@angular/forms';
import { merge } from 'rxjs';
import { CustomValueAccessor } from '../custom-value-accessor';
import { RadioOptionComponent } from './radio-option/radio-option.component';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-radio',
templateUrl: './radio.component.html',
styleUrls: ['./radio.component.scss'],
standalone: true,
imports: [CommonModule],
})
export class RadioComponent
extends CustomValueAccessor<string>
implements AfterContentInit
{
@ContentChildren(RadioOptionComponent)
options!: QueryList<RadioOptionComponent>;
@Input() label: string | undefined;
constructor(@Self() @Optional() private ngControl: NgControl) {
super();
if (this.ngControl) this.ngControl.valueAccessor = this;
}
ngAfterContentInit(): void {
merge(...this.options.map((option) => option.selected$)).subscribe({
next: (value) => {
this.writeValue(value);
},
complete: () => {
const selectedOption = this.options.find(
(option) => option.value === this.value
);
if (selectedOption) selectedOption.checked = true;
},
});
}
}
1. input-radio 의 경우 여러개의 옵션이 필요하므로 옵션 영역과 범위 영역을 각각 컴포넌트화
/* 컴포넌트 활용 부분 */
// 템플릿
<app-radio label="권한 설정" formControlName="role">
<app-radio-option
*ngFor="let role of roles"
[value]="role"
name="role"
[checked]="admin ? role === admin.role : false"
>{{role}}</app-radio-option
>
</app-radio>
// 컴포넌트
adminForm = new FormGroup({
role: new FormControl<Roles | null>(null, [Validators.required]),
});
console.log(this.adminForm.getRawValue().role!);
2. 컴포넌트를 가져와서 직접 사용하는 부분
반응형
LIST
'IT' 카테고리의 다른 글
Angular font 적용하는 방법 예시 (0) | 2023.06.28 |
---|---|
Tailwind media query response 예시 (0) | 2023.06.28 |
TypeScript Record 레코드 StringMap 활용 예시 (0) | 2023.06.26 |
Angular Ionic 스크롤 이벤트 감지 / Angular animation 앵귤러 애니메이션 (0) | 2023.06.26 |
Typescript 배열 중복 제거 (0) | 2023.06.25 |