Skip to content

Instantly share code, notes, and snippets.

@pavanandhukuri
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pavanandhukuri/aea1e06a84afa26efafb to your computer and use it in GitHub Desktop.
Save pavanandhukuri/aea1e06a84afa26efafb to your computer and use it in GitHub Desktop.
Angular JS directive for select control. Ability to load options based on JSON data from a REST URL.
<html>
<head>
<script type="text/javascript" src="angular.min.js"></script>
<script type="text/javascript" src="restselect.js"></script>
<script type="text/javascript">
var app = angular.module('restSelectDemo', ['restselect']);
app.controller('restSelectCtrl',function($scope){});
</script>
</head>
<body ng-app="restSelectDemo">
<div ng-controller="restSelectCtrl">
<form id="frm" name="frm" ng-submit="formSubmit()">
<div>
<!-- ng-required, ng-class and other attributes can be applied on the directive -->
<rest-select ng-model="sOption" rest-url="http://jsonplaceholder.typicode.com/users"
option-label="name"/>
</div>
<br/>
<br/>
<div>
Selected Option: {{sOption}}
</div>
</form>
</div>
</body>
</html>
/*************************************************************************************
Copyright (c) 2015 Pavan Andhukuri
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
************************************************************************************/
angular.module('restselect', []).directive('restSelect', function($http){
/**
* if option-value is passed, it will be assigned as value of the option. Else, the whole object will be (exactly what ng-options does)
*/
function getTemplate(elem, attrs){
if(attrs.optionValue)
return '<select ng-options="opt.' + attrs.optionValue + ' as opt.' + attrs.optionLabel + ' for opt in opts"></select>';
else
return '<select ng-options="opt.' + attrs.optionLabel + ' for opt in opts"></select>';
}
function validateAttrs(attrs){
if(attrs.restUrl == null || attrs.restUrl == undefined)
throw ('rest-url attribute must be passed');
if(attrs.optionLabel == null || attrs.optionLabel == undefined)
throw ('option-label attribute must be passed');
}
return {
restrict : 'E',
require : 'ngModel',
replace : true,
template : getTemplate,
scope : {},
link : function(scope, elem, attrs, ngModel){
validateAttrs(attrs);
$http.get(attrs.restUrl).success(function(d){
scope.opts = d;
});
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment