I have a html select option
<select>
<option ng-repeat="field in filter.fields" value="{{field.id}}">{{field.name}}</option>
</select>
which I am iterating from ng-repeat , I want to disable option on basis of a filed selectable like
<select>
<option ng-repeat="field in filter.fields" {field.selectable==true?enable:disable} value="{{field.id}}">{{field.name}}</option>
</select>
How can I achieve this with angular?
The disabled attribute for <select> element in HTML is used to specify that the select element is disabled. A disabled drop-down list is un-clickable and unusable. It is a boolean attribute.
In your <ng-option> , put a [disabled] attribute with some conditional in it.
Disabling the select or individual options It is possible to disable the entire select or individual options in the select by using the disabled property on the <select> or <mat-select> and the <option> or <mat-option> elements respectively.
get('yourPath'). disable({onlySelf: true}); If not, you can also just use the [disabled]="isFieldDisabled" property on the <select> property. If you use a FormGroup with your DropDownField but choose to disable the DropDownField via [disabled]="..." , then you will get much yellow text in your console.
Assuming you have a structure like this:
$scope.filter = {
fields: [
{id: 1, name: "a", selectable: false},
{id: 2, name: "asdf", selectable: true},
{id: 3, name: "qwet", selectable: false},
{id: 4, name: "qnjew", selectable: true},
{id: 5, name: "asdjf", selectable: false}
]
};
This should work for you:
<select>
<option ng-repeat="field in filter.fields" ng-disabled="field.selectable" value="{{field.id}}">{{field.name}}</option>
</select>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With