0
In html
<select class="element-margin-top" ng-model="vm.selectedRole" ng-options="(roleName,enabled) in vm.roleNames">
                <option value="">All Roles</option>{{vm.roles[0]}}
            </select>

I want to display all the array elements in select options.I dont understand what is going on in this.It is giving me error related to (roleName,enabled) in vm.roleNames

In js:
 var vm = this;
 vm.roleNames = ['SuperAdmin','Admin','Retailer','Manufacturer'];
 vm.selectedRole = vm.roleNames[-1];

I want first element to be selected by default.

3 Answers 3

2

ng-options="(roleName,enabled) in vm.roleNames" in this case, vm.roleNames is an array, so, roleName is the index and enabled is the real "roleName"

So your code should looks like this:

<select class="element-margin-top" ng-model="vm.selectedRole" ng-options="(index,roleName) as roleName in vm.roleNames">
         <option value="">All Roles</option>
</select>
Sign up to request clarification or add additional context in comments.

2 Comments

ok thanks but how I am supposed to pull elements from array to select - option
error: [ngOptions:iexp] Expected expression in form of 'select (as label)? for (key,)?_value_ in collection' but got '(index,roleName) in vm.roleNames'.
0

try this , for first selected by defult you can use $first

<select   ng-model="selectedRole">
                        <option ng-repeat="name in roleNames "
                            ng-selected="$first" value="{{name}}">{{name}}</option>
                </select>

1 Comment

I forgot that , use $first for default first selected
0

In html:

<select class="element-margin-top" ng-model="vm.selectedOption">
                    <option value="">All Roles</option>
                    <option ng-repeat="item in vm.optionNames" value="{{item}}">{{item}}</option>
                </select>

in js:-

vm.optionNames = ['User', 'Account', 'Brand'];
  vm.selectedOption = vm.optionNames[-1];

try reading http://www.undefinednull.com/2014/08/11/a-brief-walk-through-of-the-ng-options-in-angularjs/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.