JSFiddle - React, Tailwind, and code Playground
by alalonde
HTML
<script src="http://code.angularjs.org/1.1.0/angular.js"></script>
<div ng-controller="OptionsController">
<select ng-model="selectedport"
ng-options="p.name as p.name for p in ports"
options-disabled="p.isinuse for p in ports"></select>
</div>
JavaScript
angular.module('myApp', [])
.directive('optionsDisabled', function($parse) {
return {
priority: 0,
require: 'ngModel',
link: function(scope, iElement, iAttrs, ctrl) {
scope.disableOptions = function(attr, element, data, fnDisableIfTrue) {
$("option", element).each(function(i) {
var locals = {};
locals[attr] = data[i];
$(this).attr("disabled", fnDisableIfTrue(scope, locals));
});
};
// parse expression and build array of disabled options
var expElements = iAttrs.optionsDisabled.match(/(.+)\s+for\s+(.+)\s+in\s+(.+)/);
var attrToWatch = expElements[3];
var fnDisableIfTrue = $parse(expElements[1]);
scope.$watch(attrToWatch, function(newValue, oldValue) {
if(newValue)
scope.disableOptions(expElements[2], iElement, newValue, fnDisableIfTrue);
});
}
};
});
function OptionsController($scope) {
$scope.ports = [{name: 'http', isinuse: true},
{name: 'test', isinuse: false}];
$scope.selectedport = 'test';
}