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 |
|---|---|---|
| 2045 | 2025. 11. 30. | TypeScript 타입 시스템 완벽 가이드 |
| 2044 | 2025. 11. 30. | React 18 새 기능 - Concurrent Features |
| 1864 | 2022. 07. 27. | [vue3] vue3 setup 에서 자식 컴포넌트 메소드(함수) 호출하기 |
| 1848 | 2022. 06. 11. | [ vuejs ] router 데이터 전달방식 |
| 1847 | 2022. 06. 11. | [ vuejs ] this.$router.push 혹은 link-to 를 setup() 에서 구현 |
| 1846 | 2022. 06. 10. | [ vuejs3 , spring-boot ] 웹개발은 어떻게 구성해야 하는가 |
| 1825 | 2022. 05. 24. | [ vuejs ] 부모,자식 요소의 메소드 호출 |
| 1421 | 2018. 12. 06. | [ angualrjs ] ajax header 포함시켜 $http 사용하기 |
| 1381 | 2017. 11. 30. | 문자열을 숫자로 바꾸는 방법 parseInt 와 Number 비교 |
| 1220 | 2017. 03. 28. | [ AngularJs] bootstrap nav dropdown |