I have this AngularJS Web App that i'm building Here (Only the Entrees > Breadsticks have items currently)
Everything was fine, until I decided that I wanted to allow the user to select different size items (either large, medium or small). This task has been a headache for me because now I need to keep track of different prices, different sizes, and different number of orders for each size.
Currently i'm trying to build a function that will return the correct price depending depending on which select option is currently selected.
Here is my breadsticks object that we're working with:
$scope.breadsticks = [
{
name: 'Garlic Buttered',
priceSM: 2.99,
priceMD: 3.99,
priceLG: 4.99,
sizeSM: '6pcs',
sizeMD: '10pcs',
sizeLG: '14pcs',
description: 'Garlic buttery goodness on a stick of cheesy bread.',
numOrderSM: 0,
numOrderMD: 0,
numOrderLG: 0,
},
{
name: 'Mozzarella Stuffed',
priceSM: 3.49,
priceMD: 4.49,
priceLG: 5.49,
sizeSM: '6pcs',
sizeMD: '10pcs',
sizeLG: '14pcs',
description: 'Jam packed with that mozarella goodness that we know you love.',
numOrderSM: 0,
numOrderMD: 0,
numOrderLG: 0,
}
];
Markup
<tbody ng-repeat="breadstick in breadsticks">
<tr>
<td>
<select id="sizeSelect">
<option>Choose Size</option>
<option value="1">Small - {{breadstick.sizeSM}}</option>
<option value="2">Medium - {{breadstick.sizeMD}}</option>
<option>Large - {{breadstick.sizeLG}}</option>
</select>
</td>
<td>
{{breadstick.name}}
</td>
<td>
<script>
getPrice();
</script>
</td>
Here is my attempt:
$scope.getPrice = function() {
var selectList = document.getElementById("sizeSelect");
var selectSize = selectList.options[selectList.selectedIndex].value;
if(selectSize == 1) {
return breadstick.priceSM;
} else if(selectSize == 2) {
return breadstick.priceMD;
}
};
I'm having problems calling this function, even though I realize now that it is not reusable because it will always return breadstick prices... May be able to add a parameter somehow to make it reusable.
The error is getPrice() is undefined since it is in scope, but $scope.getPrice(); is also undefined. I need to print to the screen a different value depending which select option is chosen.