Contents
see List작성일 2016. 08. 18.
AngularJS ng-options 완벽 가이드
ng-options는 AngularJS에서 select 드롭다운을 동적으로 생성하는 디렉티브입니다. ng-repeat보다 성능이 우수하고 객체 바인딩을 지원합니다.
1. 기본 문법
<!-- 배열에서 옵션 생성 -->
<select ng-model="selectedItem"
ng-options="item for item in items">
</select>
<!-- 기본 선택 옵션 추가 -->
<select ng-model="selectedItem"
ng-options="item for item in items">
<option value="">선택하세요</option>
</select>
2. 객체 배열 사용
// Controller
$scope.users = [
{id: 1, name: "홍길동", dept: "개발팀"},
{id: 2, name: "김철수", dept: "기획팀"},
{id: 3, name: "이영희", dept: "디자인팀"}
];
<!-- 표시값 as 실제값 for 아이템 in 배열 -->
<select ng-model="selectedUser"
ng-options="user.name for user in users">
</select>
<!-- ID를 값으로, 이름을 라벨로 -->
<select ng-model="selectedUserId"
ng-options="user.id as user.name for user in users">
</select>
3. 그룹화 (group by)
<!-- 부서별 그룹화 -->
<select ng-model="selectedUser"
ng-options="user.name group by user.dept for user in users">
</select>
<!-- 결과 -->
<optgroup label="개발팀">
<option>홍길동</option>
</optgroup>
<optgroup label="기획팀">
<option>김철수</option>
</optgroup>
4. 정렬 및 필터링
<!-- 이름순 정렬 -->
<select ng-model="selected"
ng-options="u.name for u in users | orderBy:name">
</select>
<!-- 필터링 -->
<select ng-model="selected"
ng-options="u.name for u in users | filter:{dept:개발팀}">
</select>
5. 비활성화 옵션
<!-- disable when 조건 -->
<select ng-model="selected"
ng-options="u.name disable when u.inactive for u in users">
</select>
ng-options vs ng-repeat 비교
| 구분 | ng-options | ng-repeat |
|---|---|---|
| 성능 | 우수 (최적화됨) | 상대적 느림 |
| 객체 바인딩 | 지원 | 문자열만 |
| 빈 옵션 | 별도 추가 필요 | 함께 작성 |
| track by | 지원 | 지원 |
주의사항
- ng-model 필수 - 없으면 동작하지 않음
- 초기값 설정시 참조 일치 필요
- track by로 성능 최적화 가능
javascript
| No | 작성일 | Title |
|---|---|---|
| 3384 | 2026. 08. 21. | Node.js API에서 요청 취소를 제대로 처리하는 방법: AbortSignal로 외부 작업 정리하기 |
| 3352 | 2026. 08. 13. | Node.js 응답이 간헐적으로 느릴 때: 이벤트 루프 지연 측정과 CPU 작업 분리 방법 |
| 3320 | 2026. 08. 04. | JavaScript fetch 요청을 안전하게 만드는 방법: timeout·재시도·오류 처리 실전 패턴 |
| 3288 | 2026. 07. 27. | Node.js 서버 무중단 배포를 위한 Graceful Shutdown 구현 가이드 |
| 3257 | 2026. 07. 19. | Node.js 대용량 파일 처리 가이드: 스트림과 backpressure로 메모리 사용량 제어하기 |
| 3232 | 2026. 07. 11. | Node.js 실무형 에러 처리: async/await에서 실패를 일관되게 분류하고 복구 속도 높이기 |
| 3174 | 2026. 07. 02. | Node.js 환경 변수 검증 가이드: process.env를 안전한 설정 객체로 바꾸기 |
| 3123 | 2026. 06. 24. | TypeScript 타입 좁히기 운영 가이드: unknown 입력을 안전한 도메인 객체로 바꾸기 |
| 3065 | 2026. 06. 16. | 브라우저 성능 병목 찾기: PerformanceObserver로 LCP·INP·긴 작업 로그 수집하기 |
| 2978 | 2026. 06. 08. | Node.js fetch 타임아웃과 재시도 설계: 외부 API 장애 전파 줄이기 |