I have a number of hidden input fields on a page. I want to read the values of these input fields into an array. The input fields have this structure:
<div class="loadedFood" ng-model="savedFood">
<div class="foodItem">
<input type="hidden" name="[food][61][uid]" value="61" />
<input type="hidden" name="[food][61][unit]" value="30.00" />
<input type="hidden" name="[food][61][quantity]" value="4" />
<input type="hidden" name="[food][61][total]" value="120" />
</div>
<div class="foodItem">
<input type="hidden" name="[food][67][uid]" value="67" />
<input type="hidden" name="[food][67][unit]" value="7.00" />
<input type="hidden" name="[food][67][quantity]" value="6" />
<input type="hidden" name="[food][67][total]" value="42" />
</div>
</div>
I want to read these into an array loadedFood with each div fooditem becoming a new array element, containing the names and values of the hidden inputs, like:
[{
uid : 61,
unit : 7.00,
quantity : 6,
total : 42,
} {
uid : 67,
unit : 3.00,
quantity : 3,
total : 42,
}]
I can push the value of each individual input on the page to the array loadedFood like this:
$scope.savedFood = angular.forEach(angular.element(".foodItem input"), function (value, key) {
var a = angular.element(value);
value = a.attr("value");
console.log(value)
$scope.loadedFood.push(value);
});
But obviously that's not what I want. I need to create a new element for each .foodItem, and then add both the name and the value of each input within it, and then push the whole element to loadedFood. I'm stuck on the very basic problem of how DOM element selectors work in AngularJS- trying to do something JQueryish like
var input = angular.element(value + " input");
totally breaks.
First of all, is doing this sort of a DOM-element-iteration too much of trying to apply a JQuery approach to Angular? Is there a better Angular method of getting what I need, which is the data in the hidden inputs in the form of the array above?
Secondly, how do you extend element selectors in AngularJS- if you have element a, how do you select for inputs contained within a?