Angular Selector

Angular Selector

Angular Selector is a native AngularJS directive that transform a simple <select> box into a full html select with typeahead.

  • No-jQuery required

Features

Options

Check out all the available options here.

Examples

  • Chrome
  • Firefox
  • Safari
  • Internet Explorer

Current value: "GC"

<select selector
	model="browser"
	options="browsers"
	value-attr="value"></select>

<p>
	Current value: <code ng-bind="browser|json"></code>
</p>
$scope.browser = "GC";

$scope.browsers = [
	{ value: "GC", label: "Chrome" },
	{ value: "FF", label: "Firefox" },
	{ value: "AS", label: "Safari" },
	{ value: "IE", label: "Internet Explorer" }
];
  • Firefox
  • Internet Explorer

Current value: [ "GC", "AS" ]

<select selector
	multi="true"
	model="myBrowsers"
	options="browsers"
	value-attr="value"></select>

<p>
	Current value: <code ng-bind="myBrowsers|json"></code>
</p>
$scope.myBrowsers = [ "GC", "AS" ];

$scope.browsers = [
	{ value: "GC", label: "Chrome" },
	{ value: "FF", label: "Firefox" },
	{ value: "AS", label: "Safari" },
	{ value: "IE", label: "Internet Explorer" }
];
  • Chrome
  • Firefox
  • Safari
  • Internet Explorer

Current value: { "name": "Firefox" }

<!-- just DON'T add `value-attr` attribute! -->
<select selector
	model="browser"
	options="browsers"
	label-attr="name"></select>

<p>
	Current value: <code ng-bind="browser|json"></code>
</p>
$scope.browser = { name: "Firefox" };

$scope.browsers = [
	{ name: "Chrome" },
	{ name: "Firefox" },
	{ name: "Safari" },
	{ name: "Internet Explorer" }
];
  • Chrome
  • Firefox
  • Internet Explorer

Current value: [ "AS" ]

Icons: Logotypes

<script type="text/ng-template" id="selector/demo/browserWithURL">
	{{option.label}}
	<a class="small" ng-href="{{option.url}}" ng-bind="option.url"></a>
</script>

<script type="text/ng-template" id="selector/demo/browserWithIcon">
	<img ng-src="{{option.icon}}"> {{option.label}}
</script>

<select selector
	multi="true"
	model="myBrowsers"
	options="browsers"
	value-attr="code"
	view-item-template="'selector/demo/browserWithURL'"
	dropdown-item-template="'selector/demo/browserWithIcon'"></select>

<p>
	Current value: <code ng-bind="myBrowsers|json"></code>
</p>

<p class="small">
	Icons:
	<a href="https://www.iconfinder.com/iconsets/logotypes" target="_blank">
		Logotypes
	</a>
</p>
$scope.myBrowsers = [ "AS" ];

$scope.browsers = [
	{
		code: "GC",
		label: "Chrome",
		icon: "https://cdn1.iconfinder.com/data/icons/logotypes/32/chrome-24.png",
		url: "https://www.google.it/chrome"
	},
	{
		code: "FF",
		label: "Firefox",
		icon: "https://cdn1.iconfinder.com/data/icons/logotypes/32/firefox-24.png",
		url: "https://www.mozilla.org/firefox"
	},
	{
		code: "AS",
		label: "Safari",
		icon: "https://cdn1.iconfinder.com/data/icons/logotypes/32/safari-24.png",
		url: "http://www.apple.com/safari/"
	},
	{
		code: "IE",
		label: "Internet Explorer",
		icon: "https://cdn1.iconfinder.com/data/icons/logotypes/32/internet-explorer-24.png",
		url: "http://windows.microsoft.com/internet-explorer"
	}
];
  •   Europe
  •   EUR - Euro Member Countries
  •   Europe
  • £  GBP - United Kingdom Pound
  •   Europe
  • kr  SEK - Sweden Krona
  •   Others
  • $  USD - United States Dollar
  •   Others
  • ¥  EUR - Japan Yen

Current value: "5"

Icons: FatCow Hosting Additional Icons

Hide/show all options

[
  {
    "code": "EUR",
    "symbol": "€",
    "value": "2",
    "label": "Euro Member Countries",
    "zone": "Europe"
  },
  {
    "code": "GBP",
    "symbol": "£",
    "value": "3",
    "label": "United Kingdom Pound",
    "zone": "Europe"
  },
  {
    "code": "SEK",
    "symbol": "kr",
    "value": "5",
    "label": "Sweden Krona",
    "zone": "Europe"
  },
  {
    "code": "USD",
    "symbol": "$",
    "value": "1",
    "label": "United States Dollar",
    "zone": "Others"
  },
  {
    "code": "EUR",
    "symbol": "¥",
    "value": "4",
    "label": "Japan Yen",
    "zone": "Others"
  }
]

<script type="text/ng-template" id="selector/demo/currency">
	<kbd ng-bind="option.symbol"></kbd>&nbsp;
	<b ng-bind="option.code"></b> - {{option.label}}
</script>

<script type="text/ng-template" id="selector/demo/currencyGroup">
	<img class="currency-group"
		ng-src="http://files.softicons.com/download/web-icons/fatcow-hosting-additional-icons-by-fatcow/png/32x32/{{
			option.zone == 'Europe' ? 'flag_european_union' : 'wallet'
		}}.png">&nbsp;
	{{option.zone}}
</script>

<select selector
	model="currency"
	options="currencies"
	value-attr="value"
	group-attr="zone"
	view-item-template="'selector/demo/currency'"
	dropdown-item-template="'selector/demo/currency'"
	dropdown-group-template="'selector/demo/currencyGroup'">
	
	<optgroup label="Europe">
		<option value="2"
			data-code="EUR"
			data-symbol="€">Euro Member Countries</option>
		<option value="3"
			data-code="GBP"
			data-symbol="£">United Kingdom Pound</option>
		<option value="5"
			data-code="SEK"
			data-symbol="kr"
			selected>Sweden Krona</option>
	</optgroup>
	<optgroup label="Others">
		<option value="1"
			data-code="USD"
			data-symbol="$">United States Dollar</option>
		<option value="4"
			data-code="EUR"
			data-symbol="¥">Japan Yen</option>
	</optgroup>
	
</select>

<p>
	Current value: <code ng-bind="currency|json"></code>
</p>

<p class="small">
	Icons:
	<a href="http://www.softicons.com/web-icons/fatcow-hosting-additional-icons-by-fatcow" target="_blank">
		FatCow Hosting Additional Icons
	</a>
</p>

<p ng-init="show=false">
	<a ng-click="show=!show">Hide/show all options</a>
	<pre ng-show="show" ng-bind="currencies|json"></pre>
</p>
  • PHP
  • Java
  • Ruby
  • Node

Current value:

<label class="checkbox-inline">
	<input type="checkbox" ng-model="multi"> Multiple
</label>
<label class="checkbox-inline">
	<input type="checkbox" ng-model="rtl"> RTL
</label>
<label class="checkbox-inline">
	<input type="checkbox" ng-model="remove"> Remove button
</label>
<label class="checkbox-inline">
	<input type="checkbox" ng-model="restore"> Restore on backspace
</label>
<label class="checkbox-inline">
	<input type="checkbox" ng-model="disabled"> Disabled
</label>

<select selector
	rtl="rtl"
	multi="multi"
	model="language"
	disable="disabled"
	options="languages"
	value-attr="value"
	label-attr="value"
	remove-button="remove"
	soft-delete="restore"
	placeholder="Choose your favourite language(s)...">
	
	<option value="PHP"></option>
	<option value="Java"></option>
	<option value="Ruby"></option>
	<option value="Node"></option>
	
</select>

<p>
	Current value: <code ng-bind="language|json"></code>
</p>

Current value: []

Icons: Arnoud-B/csscountrycodes

<link rel="stylesheet" href="https://rawgit.com/Arnoud-B/csscountrycodes/master/flags.css">

<script type="text/ng-template" id="selector/demo/country">
	<i class="flag" ng-class="option.code.toLowerCase()"></i>&nbsp;
	{{option.name}}
</script>

<select selector
	multi="true"
	model="countries"
	remote="remoteConfig"
	remote-param="text"
	value-attr="code"
	view-item-template="'selector/demo/country'"
	dropdown-item-template="'selector/demo/country'"
	placeholder="Choose one or more countries..."></select>

<p>
	Current value: <code ng-bind="countries|json"></code>
</p>

<p class="small">
	Icons:
	<a href="https://github.com/Arnoud-B/csscountrycodes" target="_blank">
		Arnoud-B/csscountrycodes
	</a>
</p>
$scope.remoteConfig = {
	url: "http://services.groupkt.com/country/search",
	transformResponse: function (data) {
		var countries = angular.fromJson(data).RestResponse.result;
		return countries.map(function (country) {
			return {
				name: country.name,
				code: country.alpha2_code
			};
		});
	}
};

Current value: "SV"

Icons: Arnoud-B/csscountrycodes

<link rel="stylesheet" href="https://rawgit.com/Arnoud-B/csscountrycodes/master/flags.css">

<script type="text/ng-template" id="selector/demo/country">
	<i class="flag" ng-class="option.code.toLowerCase()"></i>&nbsp;
	{{option.name}}
</script>

<select selector
	model="country"
	value-attr="code"
	debounce="200"
	remote="remote"
	remote-param="{{remoteParam}}"
	remote-validation="remoteValidation(value)"
	view-item-template="'selector/demo/country'"
	dropdown-item-template="'selector/demo/country'"
	placeholder="Choose one or more countries..."></select>

<p>
	Current value: <code ng-bind="country|json"></code>
</p>

<p class="small">
	Icons:
	<a href="https://github.com/Arnoud-B/csscountrycodes" target="_blank">
		Arnoud-B/csscountrycodes
	</a>
</p>
var options = {
	url: 'http://services.groupkt.com/country/',
	method: 'GET',
	cache: true,
	transformResponse: function (data) {
		var result = angular.fromJson(data).RestResponse.result;
		if (!angular.isArray(result)) result = [result];
		return result.map(function (country) {
			return {
				name: country.name,
				code: country.alpha2_code
			};
		});
	}
};

$scope.country = 'SV';

$scope.remote = angular.copy(options);
$scope.remote.url += 'search';
$scope.remoteParam = 'text';

$scope.remoteValidation = function (value) {
	var settings = angular.copy(options);
	settings.url += 'get/iso2code/' + value;
	return settings;
}

Open this example on

Current value: []

<div class="btn-group">
	<button class="btn btn-default" ng-click="countriesAPI.open()">
		Open
	</button>
	<button class="btn btn-default" ng-click="countriesAPI.focus()">
		Focus
	</button>
	<button class="btn btn-default" ng-click="countriesAPI.close()">
		Close
	</button>
	<button class="btn btn-default" ng-click="countriesAPI.fetch()">
		Fetch
	</button>
	<button class="btn btn-default" ng-click="countriesAPI.set('SXM')">
		Set <code>SXM</code>
	</button>
	<button class="btn btn-default" ng-click="countriesAPI.set('ITA')">
		Set <code>ITA</code>
	</button>
	<button class="btn btn-default" ng-click="countriesAPI.unset('ITA')">
		Unset <code>ITA</code>
	</button>
	<button class="btn btn-default" ng-click="countriesAPI.unset()">
		Unset all
	</button>
</div>

<select selector
	model="countries"
	multi="true"
	api="countriesAPI"
	remote="remoteConfig"
	remote-param="text"
	label-attr="name"
	value-attr="alpha3_code"
	placeholder="Choose one or more countries..."></select>

<p>
	Current value: <code ng-bind="countries|json"></code>
</p>
$scope.remoteConfig = {
	cache: false,
	url: "http://services.groupkt.com/country/search",
	transformResponse: function (data) {
		return angular.fromJson(data).RestResponse.result;
	}
};
  • Nile
  • Amazon
  • Mississippi
  • Yangtze
  • Ob
  • Yellow
  • Yenisei
  • Paraná
  • Irtish
  • Chambeshi
River
Nile
Length
6,690 km
Outflow
Mediterranean

Current value: { "name": "Nile", "length": 6690, "outflow": "Mediterranean" }

<div class="btn-group">
	<button class="btn btn-default"
		ng-class="{active:zone=='global'}"
		ng-click="zone='global'">
		Global Rivers
	</button>
	<button class="btn btn-default"
		ng-class="{active:zone=='european'}"
		ng-click="zone='european'">
		European Rivers
	</button>
</div>

<select selector
	model="river"
	options="rivers"
	label-attr="name"
	placeholder="Select a river..."></select>

<dl class="dl-horizontal">
	<dt>River</dt>
	<dd ng-bind="river.name || '-'"></dd>
	
	<dt>Length</dt>
	<dd ng-bind="river.length ? (river.length | number) + ' km' : '-'"></dd>
	
	<dt>Outflow</dt>
	<dd ng-bind="river.outflow || '-'"></dd>
</dl>

<p>
	Current value: <code ng-bind="river|json"></code>
</p>
$scope.zone = "global";

$scope.globalRivers = [
	{ name: "Nile",        length: 6690, outflow: "Mediterranean" },
	{ name: "Amazon",      length: 6296, outflow: "Atlantic Ocean" },
	{ name: "Mississippi", length: 5970, outflow: "Gulf of Mexico" },
	{ name: "Yangtze",     length: 5797, outflow: "China Sea" },
	{ name: "Ob",          length: 5567, outflow: "Gulf of Ob" },
	{ name: "Yellow",      length: 4667, outflow: "Gulf of Chihli" },
	{ name: "Yenisei",     length: 4506, outflow: "Arctic Ocean" },
	{ name: "Paraná",      length: 4498, outflow: "Río de la Plata" },
	{ name: "Irtish",      length: 4438, outflow: "Ob River" },
	{ name: "Chambeshi",   length: 4371, outflow: "Atlantic Ocean" }
];

$scope.europeanRivers = [
	{ name: "Volga",       length: 3692, outflow: "Caspian Sea" },
	{ name: "Danube",      length: 2860, outflow: "Black Sea" },
	{ name: "Ural",        length: 2428, outflow: "Caspian Sea" },
	{ name: "Dnieper",     length: 2290, outflow: "Black Sea" },
	{ name: "Don",         length: 1950, outflow: "Sea of Azov" },
	{ name: "Pechora",     length: 1809, outflow: "Arctic Ocean" },
	{ name: "Kama",        length: 1805, outflow: "Volga" },
	{ name: "Oka",         length: 1500, outflow: "Volga" },
	{ name: "Belaya",      length: 1430, outflow: "Kama" },
	{ name: "Dniester",    length: 1362, outflow: "Black Sea" }
];

$scope.$watch('zone', function (zone) {
	$scope.rivers = $scope[zone + 'Rivers'];
	// select first
	$scope.river = $scope.rivers[0];
});
  • Basketball
  • Videogames
  • Travelling

Current value: []

<select selector
	model="myHobbies"
	multi="true"
	options="hobbies"
	value-attr="value"
	placeholder="Which are your hobbies? (type something that is not in the list)"
	create="createFunction(input)"></select>

<p>
	Current value: <code ng-bind="myHobbies|json"></code>
</p>
$scope.hobbies =[
	{ value: 0, label: "Basketball" },
	{ value: 1, label: "Videogames" },
	{ value: 2, label: "Travelling" }
];

$scope.createFunction = function (input) {
	// format the option and return it
	return {
		value: $scope.hobbies.length,
		label: input
	};
};
  • Basketball
  • Videogames
  • Travelling

Current value: []

<div ng-show="!creation.active">
	<select selector
		model="myHobbies"
		multi="true"
		options="hobbies"
		value-attr="value"
		placeholder="Which are your hobbies? (type something that is not in the list)"
		create="creation.show(input)"></select>
	
	<p>
		Current value: <code ng-bind="myHobbies|json"></code>
	</p>
</div>

<form ng-show="creation.active" ng-submit="creation.insert()">
	<div class="form-group">
		<label>Value</label>
		<input ng-model="creation.value" required autofocus
			placeholder="value" class="form-control">
	</div>
	<div class="form-group">
		<label>Label</label>
		<input ng-model="creation.label" required
			placeholder="label" class="form-control">
	</div>
	<button type="submit" class="btn btn-success">
		Insert value!
	</button>
	<button type="button" class="btn btn-default"
		ng-click="creation.cancel()">
		Cancel
	</button>
</form>
$scope.hobbies = [
	{ value: 0, label: "Basketball" },
	{ value: 1, label: "Videogames" },
	{ value: 2, label: "Travelling" }
];

$scope.creation = {
	active: false,
	deferred: null,
	show: function (input) {
		this.deferred = $q.defer();
		this.active   = true;
		this.value    = $scope.hobbies.length;
		this.label    = input;
		return this.deferred.promise;
	},
	insert: function () {
		this.active = false;
		this.deferred.resolve({
			value: this.value,
			label: this.label
		});
	},
	cancel: function () {
		this.active = false;
		this.deferred.reject();
	}
};