Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

Given the following select element

<select ng-options="size.code as size.name for size in sizes "

       ng-model="item.size.code" 

       ng-change="update(MAGIC_THING)"> 

</select>

Is there a way to get MAGIC_THING to be equal to the currently selected size, so I have access to size.name and size.code in my controller?

size.code affects a lot of other parts of the app (image URLs, etc), but when the ng-model of item.size.code is updated, item.size.name needs to be updated as well for the user-facing stuff. I assume that the correct way to do this is capturing the change event and setting the values inside of my controller, but I'm not sure what I can pass into an update to get the proper values.

If this is completely the wrong way to go about it, I'd love to know the right way.

1 Answer

0 votes
by (106k points)

For getting the ng-object which is selected with ng-change you can use some other $scope property for your select ng-model then:

<select ng-options="size as size.name for size in sizes" ng-model="selectedItem" ng-change="update()"></select>

Controller code:

$scope.update = function() {
$scope.item.size.code = $scope.selectedItem.code 

}

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...