Support disabling options in select with ng:options
Need a way to construct
<select>
<option disabled>A</option>
<option>B</option>
<optionC</option>
</select>
There's no way to express the disabled state of a specific option with ng:options
+1
+1, the syntax could be
select as label with options for value in arrayWith options being an hash like: {disabled: product.available}
See actual documentation.
this syntax make sence
Hello everybody, I'm working on a PR to make ng-options support disable.
So far we have two syntax proposals:
select as label with options for value in array
by @Dorian, and
select as label disable if cond group by group for value in array
by @cades.
Which one do you prefer and why, or there is another syntax proposal?
I found a problem in syntax
select as label with options for value in array
If there's code like this:
<select ng-options="product.id as product.name with options for product in products"></select>
function Ctrl($scope){
$scope.options = {
disabled: product.available
};
$scope.products = [
{id:1, name: 'P1', available: true},
{id:2, name: 'P2', available: false}
];
}
$scope.options cannot be an hash like: {disabled: product.available}, because product does not live in the scope of where $scope.options is declared.
However, it can be a string like: 'product.available', or a callback like
$scope.options = {
disabled: function(product){
return product.available;
}
};
@cades awesome!
I think select as label disable if cond group by group for value in array is the way to go as it's more obvious from the statement what is happening. No having to jump through other files just to see why something is being disabled, you can see it right there
Have you thought about using an additional attribute, like ng-options-disabled?
@aronduby @vikasgulati has provided one.
@cades yes, but it doesn't work with group-by
@aronduby Yes it doesn't :( An separate directive need modification to ng-options, too.
@cades I vote for the select as label disable if cond group by group for value in array option, as I cannot personally think of anything else you would need to do to an option that isn't already covered by existing functionality. Conditional disabling is the last major functionality gap for ng-options, and I am excited by the idea of your PR, especially if it got accepted into core.
Now the PR is ready (disable if version), however I found another issue: what if the default option (initial value set on $scope, which binds to select's ng-model) is disabled?
Should we prevent user from doing this, or respect user's intention?
The possible behavior may be:
- select it (as user wish)
- use null option, and (optionally) print warning message to console
- throw an exception, forcing user to resolve it
@cades i think it should be the same behavior as plain HTML: select it ( http://jsfiddle.net/006vyh0b/ )
@mathroc++ And this is the default behavior (without extra work)
great job!
+1
+1
Currently using @vikasgulati solution, but notice that it doesn't support dynamic options. There needs to be a watch on the first argument so that if that changes, the disabled attribute will be updated.
+1
:+1: any good news on this ?
+1
+1