Search code examples
javascriptangularjsng-optionsangularjs-ng-options

ng-change works incorrect?


I have one code:

<select ng-options="i.id as i.os_version for i in devices"
        ng-model="selected_version_os_to" 
        ng-change="selectVersion(i.id)">
    <option value=''> Select version</option>
</select>

Why I get undefined in console when try to display value in:

ng-change="selectVersion(i.id)"?

Inside selectVersionfunction: I get i.id as undefined:

$scope.selectVersion = function(item) { 
    console.log(item); 
}

Solution

  • You don't need to pass anything in selectVersion method, as ng-model already binding selected value for you.

    Your code should be like:

    <select ng-options="i.id as i.os_version for i in devices"
            ng-model="selected_version_os_to" 
            ng-change="selectVersion()">
        <option value=''> Select version</option>
    </select>
    
    
    // controller code
    $scope.selectVersion = function() { 
        console.log($scope.selected_version_os_to); 
    }