Social network
Social network

Reputation: 1

How to get selected value index dynamically using ng-options in angularjs

I am using select, in which I require selected value and dynamically created index (I don't want to set default to [0] index) How it can be done.

        <div class="input-label">
          Select Mobile
        </div>

        <select ng-model="mobile" ng-options="mobile as mobile.value for mobile in editContactModalData.phoneNumbers"></select>
      </label>

    Above is select option and below is input box where the selected value is set       


      <label class="item item-input item-stacked-label" ng-show="mobile">
        <input type="tel" maxlength="10" placeholder="mobile" ng-model="mobile.value">
      </label>

this is one modal and I want to show these changes of selected mobile in another modal

the second modal consist

  <span>{{contact.phoneNumbers[0].value}}</span>

so in this in the place of [0] index i want to put dynamically selected value.

Upvotes: 0

Views: 907

Answers (2)

yasir_mughal
yasir_mughal

Reputation: 112

You can use ng-change and assign value to contact like

<select ng-change="setNumber(mobile)" ng-model="mobile" ng-options="mobile as mobile.value for mobile in editContactModalData.phoneNumbers"></select>

In controller just set value:

$scope.setNumber = function(num){

$scope.contact = num.value;

}

Then this will show in view

{{contact}}

Upvotes: 0

Deepak Patidar
Deepak Patidar

Reputation: 394

@Nikhil: First thing ngOptions doesn't support $index like ng-repeat.

If you needed index of that value than you can try like this.

<select 
  ng-model="select"
  ng-options="values.indexOf(selectedItem) as selectedItem for 
selectedItem in values">
</select>

Upvotes: 1

Related Questions