user99999
user99999

Reputation: 2022

Get option's value on change

I want to get the current option's value on select change, and I have the following code:

<select name="general[intervalSynchronization]" 
        id="intervalSynchronization" 
        class="chosen-select-no-single"
        ng-model="synchronizationTimeSelectedItem"
        ng-change="changeSynchronizationTime(synchronizationTimeSelectedItem)"
        style="width: 100%;">
    <option value="1">Don't synchronize</option>
    <option value="2">0.5 hour</option>
    <option value="3">Twice a day</option>
    <option value="4">Once a day</option>
</select>

And I console log the value inside the JS code:

$scope.changeSynchronizationTime = function (selectedItem) {
    console.log(selectedItem);
};

But it seems to output random values on the second and more change.

What's wrong?

Upvotes: 0

Views: 44

Answers (1)

Vineet
Vineet

Reputation: 4635

You don't need to pass model reference in ng-change function. change in html

ng-change="changeSynchronizationTime()"

And in controller, you can get

$scope.changeSynchronizationTime = function () {
    console.log($scope.synchronizationTimeSelectedItem);
};

angular will bind the selected value to your model automatically.

Upvotes: 3

Related Questions