I have a select
dropdown I’m setting up with an ng-options
. It’s showing up fine in the HTML page, but when I select an option, the variable defined by ng-model
is not being updated in $scope
.
HTML:
<select ng-model="selected_ingredient"
ng-options="item as item.ingredient for item in available_ingredients"
ng-change="selectIngredient()">
<option value="">Add an ingredient...</option>
</select>
AngularJS:
$scope.selectIngredient = function() {
if ( $scope.debug > 0 ) {
console.log( "Selected ingredient:" );
console.log( $scope.selected_ingredient );
}
};
Console output (after dropdown item selected, $scope.debug
is 1):
Selected ingredient:
undefined
I would like $scope.selected_ingredient
to be the object item
that is displayed by the dropdown element I select. What am I doing wrong?
2
Answers
I used a workaround from @billy_comic in the comments
in my HTML as well as the following JS function
that produces the expected values. Thanks so much!
Your code appears to work for me. Maybe you’ve made a mistake in the initialization or in some part of the code you haven’t shown.