Search code examples
javascriptangularjsarraysng-options

Angular ng-option: iterate over a number array for displaying content of an other array but having index chosen in ng-model


I would like to have a dropdownlist that displays items of a string Array. I also would like to have as ng-model the index of the selected item in the dropdownlist.

I know I did not give the best description , so I write What I would be able to do.

HTML

<select ng-model="chosenPropertyIndex"
                 ng-options="arrayProperties.PropertyNames[idx] for idx in [0,1,2,3,4,5]"
        ></select>

Controller

 $scope.chosenPropertyIndex = 0;

 $scope.arrayProperties = [
    "Property A",
    "Property B",
    "Property C",
    "Property D",
    "Property E",
    "Property F"
 ];

Do you see a way to do something like that please?


Solution

  • Try this:

    <select ng-model="chosenPropertyIndex" ng-options="arrayProperties.indexOf(prop) as prop for prop in arrayProperties"></select>
    

    http://jsfiddle.net/680x5grc/