0

I'm trying to include certain options in my select box if certain expressions are true and I'm finding it adds the 1st one and not the 2nd one, and this only occurs when I use ng-options I did try this with more complex expressions but reduced to to just ng-if="true" to verify my suspicion , once I remove the ng-options both if-ng options are added.

this is my users model

    $scope.users = [{
        "id": "123",
        "description": "Tester",
        "name": "Jane"
    },
    {
        "id": "312",
        "description": "Dev",
        "name": "John"
    }];

This is my simplified select

<select class="fullWidth" ng-model="name_model" ng-selected="name_model"
ng-options="user.name as user.description for user in users" required>
    <option ng-if="true" value="">-- Please Select an Approver Type: --</option>
    <option ng-if="true" value="">-- Please Select One: --</option>
</select>

Is this a bug, is it just not allowed or am I overlooking something. I have a JSFiddle that may explain better then I am here

jonnie
  • 12,260
  • 16
  • 54
  • 91
  • possible duplicate of [Why does angularjs include an empty option in select](http://stackoverflow.com/questions/12654631/why-does-angularjs-include-an-empty-option-in-select) – Fresheyeball Aug 29 '13 at 13:36
  • @Fresheyeball it's not really a duplicate as I am looking to hardcode more options not remove an empty option, but I can see how they may relate – jonnie Aug 29 '13 at 13:46

1 Answers1

2

Basically for 2 options with ng-if = true, the code actually is equivalent to

<option value=""> Please Select an Approver Type: </option>
<option value=""> Please Select One: </option>

Since ngOptions accepts only a single hardcoded option,

Optionally, a single hard-coded element, with the value set to an empty string, can be nested into the element. This element will then represent null or "not selected" option.

the 1st one will be overwritten.

zs2020
  • 53,766
  • 29
  • 154
  • 219