I have two select, and I want the second to only show the data depending on the selection of the first one. What I lack is 'popular' the second select, because I already have the necessary data to put into it. As I'm starting with JS I do not know how to do this dynamically and I do not know if it's the best way to do it either.
HTML
<select id="selectCategory"
ng-model="selectedCategory"
ng-options="category as category.title for category in categories">
<option value="">Escolha...</option>
</select>
<select id="selectProduct"
ng-model="selectedProduct"
ng-options="product as product.name for product in products">
<option value="">Escolha...</option>
</select>
The first select is working fine, when I select an item, it calls the 'change' listener and searches for the required json file on the server with getData()
JS
var selectCategory = document.getElementById("selectCategory");
var selectProduct = document.getElementById("selectProduct");
// Aqui popula o primeiro select
DataService.getProducts().then(function (result) {
console.log(result);
$scope.categories = result.data.products;
}, function (err) {
console.error(err);
});
function getData (data){
DataService.getProduct(data).then(function (result) {
$scope.products = result;
console.log($scope.products); // <- aqui mostra o json correto
}, function (err) {
console.error(err);
});
}
// evento chamado quando escolhido uma opção no primeiro select
selectCategory.addEventListener('change', function (){
var i = selectCategory.selectedIndex -1;
getData($scope.categories[i].name);
});
Example json that is loaded
{
"product": [
{
"name": "Prod 1",
"image": "www.google.com"
},
{
"name": "Prod 2",
"image": "www.google.com"
}
]
}
What I have no idea how to do is how to put the data in the second select after picking up the data from the server