---
id: 2482
title: AngularJS... a selection-change directive... an early implementation
slug: angularjs-selection-change-directive-an-early-implementation
date: '2012-05-07T14:25:01Z'
modified: '2012-05-07T21:35:09Z'
status: publish
type: post
author: pmoelgaard
categories:
  - JavaScript
tags: []
excerpt: null
canonicalPath: /posts/angularjs-selection-change-directive-an-early-implementation
featuredImage: null
commentCount: 0
wordCount: 222
readingTimeMinutes: 1
hasMedia: false
hasVideos: false
url: >-
  https://forwardthinking.pemo.me/posts/angularjs-selection-change-directive-an-early-implementation
---
having run into some issue implementing a change handler on the select control without setting the model property... I decided to see how it could be to implement a selectionHandler directive... I started out with this implementation which obviously is far from best practice and far from ideal yet... next step will be to find the best practice to implement a tag for an entire "combobox"...

### HTML

```html
&lt;select ng-datasource=&quot;candidateContacts&quot; ng-selection-change=&quot;selectContact&quot;&gt;
	&lt;option ng-repeat=&quot;candidateContact in candidateContacts&quot; ng-selected=&quot;candidateContact.id == contact.item.id&quot;&gt;
		{{candidateContact.name}}
	&lt;/option&gt;
&lt;/select&gt;
```

### JavaScript

```javascript
.directive( &quot;ngSelectionChange&quot;, function( $compile ) {
		return function( $scope, $element, $attrs ) {

			jQuery( $element ).change( function( e ) {

				var target = jQuery( e.target );

				var datasourceKey = target.attr( &quot;ng-datasource&quot; );
				var datasource = $scope.$eval( datasourceKey);

				var actionKey = target.attr( &quot;ng-selection-change&quot; );
				var action = $scope.$eval( actionKey );

				var selectedIndex = target.prop( &quot;selectedIndex&quot; );
				var selectedItem = datasource[ selectedIndex ];

				action.call( $scope, selectedItem );
			});
		}
	})
```

UPDATE I just stumbled upon the concept of using accessor's in the directive scopes, that will help me get rid of the redundancy in the declaration of the datasource/iterator... [http://jsfiddle.net/ADukg/10/](http://jsfiddle.net/ADukg/10/) UPDATE I came across this directive when browsing the recently updated documentation system, this will do fine so there is no need to implement a ComboBox since the functionality is covered by the native select directive... [http://docs.angularjs.org/api/angular.module.ng.$compileProvider.directive.select](http://docs.angularjs.org/api/angular.module.ng.$compileProvider.directive.select)
