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 |
|---|---|---|
| 2350 | 2026. 04. 07. | ES2025 Iterator Helpers 완벽 가이드 - map, filter, take를 이터레이터에서 직접 사용하기 |
| 2336 | 2026. 04. 06. | ES2025 새 기능 실전 가이드 - Promise.try, Iterator Helpers, Set 메서드 총정리 |
| 2321 | 2026. 04. 05. | ES2026 Temporal API 완벽 가이드: Date 객체를 대체하는 새로운 표준 |
| 2296 | 2026. 04. 05. | Temporal API로 JavaScript 날짜/시간 처리 완전 정복하기 |
| 2295 | 2026. 04. 05. | ES2026 using 키워드와 Explicit Resource Management 완벽 가이드 |
| 2268 | 2026. 04. 04. | Node.js 22 완벽 가이드: 네이티브 TypeScript, WebSocket, Permission Model 실전 활용 |
| 2267 | 2026. 04. 04. | ES2025 새 기능 총정리: Promise.try, Set 메서드, RegExp 플래그까지 |
| 2216 | 2026. 02. 11. | Zustand vs Jotai: 경량 상태관리 라이브러리 비교 |
| 2215 | 2026. 02. 11. | Bun 1.x 런타임과 빌드 도구 실전 가이드 |
| 2214 | 2026. 02. 11. | TypeScript 5.x 신기능과 고급 타입 시스템 |