0

This is my code. I need to find a way to search the value in the array in $scope.options when the value of the combo box is changed.

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html ng-app>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src="../angular.js"></script>
        <script>
            function reportController($scope){
                $scope.options=[{name:'option 1', id:'1'},{name:'option 2', id:'2'},{name:'option 3', id:'3'}];

                $scope.selectedValue = {name:'option 1', id:'1'};
                $scope.updatedName = $scope.selectedValue.name;
                $scope.updatedId = $scope.selectedValue.id;

                $scope.updateCombo = function()
                {
                    console.log("inside updateCombo");

                    comboVal = document.getElementById("nameCombo").value;
                    arrIndex = $scope.options.indexOf(comboVal);
                    console.log("combo box selected value = " + comboVal + " :: index = "+ arrIndex);
                    //$scope.selectedValue=$scope.options[index];
                }
            }
        </script>
    </head>
    <body>
        <div ng-controller="reportController">
            <select id ="nameCombo" ng-model="selectedValue" ng-change="updateCombo()">
            <option ng-repeat="value in options" >{{value.name}}</option>
            </select>
            <p>Name : {{selectedValue.name}}</p>
            <p>ID : {{selectedValue.id}}</p>
        </div>
    </body>
</html>

I tried with passing the index from the function call, but that does work. I also tried indexOf, but that wont work as I only have the name and not the id from the combo box.

2 Answers 2

1

You probably want to do something like this:

<select ng-model="selectedValue" ng-options="opt.name for opt in options"></select>

The selected value is the "ng-model" and gets updated automatically.

Check this Fiddle example that I've made for you.

Sign up to request clarification or add additional context in comments.

Comments

0

You should use ng-options instead of building yourself your options:

Template:

<div ng-app ng-controller="reportController">
    <select id ="nameCombo" ng-model="selectedValue" ng-options="option.name for option in options">
    </select>
    <p>Name : {{selectedValue.name}}</p>
    <p>ID : {{selectedValue.id}}</p>
</div>

Controller:

function reportController($scope){
    $scope.options=[
        {name:'option 1', id:'1'},
        {name:'option 2', id:'2'},
        {name:'option 3', id:'3'}];

    $scope.selectedValue = $scope.options[0];
}

Here is a JSFiddle

Angular ngOptions doc

NB: $scope.selectedValue has to refer to the same object, not just be equal.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.