I'm using UI-Bootstrap so I can use them both Angular and bootstrap. I want to access the value from a select tag that the "controllerCountries" controller have and use that value as a parameter to the fill the select "selectedServices". I want to do that because the logic of the program will be that when I choose a country of a list of countries in a select tag, use that value to fill the another select tag.
My HTML is here:
<div style="margin-top: 15px" class="col-md-6">
<div id="form-pais"class="form-group">
<label class=" control-label">Country:</label>
<div class="selectContainer">
<select ng-controller="controllerCountries" class="form-control" id="abvCountry" name="abvCountry" ng-model="countrySelected">
<option value="gt">Guatemala</option>
<option value="sl">El Salvador</option>
<option value="hn">Honduras</option>
</select>
</div>
</div>
</div>
<div style="margin-top: 15px" class="col-md-6">
<div class="form-group">
<label class=" control-label">Services:</label>
<div ng-controller="controllerServices" class="selectContainer">
<select class="form-control" id="selectServices"ng-model="servicios" ng-options="y for (x, y) in serviciosgt" text="">
</select>
</div>
My JS file looks like that:
//Heres the list of services in the objects. I use this to fill the second select.
app.controller('controllerServices', function($scope)
{
$scope.serviciosgt =
{
consMiami : "Consolidación Miami",
contCompletosGT: "Contenedores Completos",
cargMartConsolidadGT : "Carga Maritima Consolidada",
cargAereaRegularGT: "Carga Áerea Regular"
};
$scope.serviciosSL =
{
contCompletosSl : "Contenedores Completos",
cargMartConsolidadSl: "Carga Marítima Consolidada",
cargAereaRegularSl: "Carga Áerea Regular"
};
$scope.serviciosHN =
{
contCompletosHN : "Contenedores Completos",
cargMartConsolidadHN: "Carga Marítima Consolidada",
cargAereaRegularHN: "Carga Áerea Regular"
};
});
//Heres the controller to fill.
app.controller('controllerCountries', function($scope)
{
$scope.countrySelected ='';
$scope.$watch('countrySelected', function () {
if($scope.countrySelected=="gt")
{
console.log("Select GT");
}
else if($scope.countrySelected=="sl")
{
console.log("Select SL");
}
else if($scope.countrySelected=="hn")
{
console.log("Select HN");
}
});
});
Right now I can access the value of the first "controllerCountries" and log that value on the console, but thats it. And, as you can see, I fill the select with the first object, but I wan them to be dinamic. Can anyone help me please. And if you look that my code is wrong, youre welcome to fix it.
Greetings and thanks.
First of all, you don't need that $watch
, you can use ng-change
to fire a callback when the select value (ng-model) is changed. you can write your if
statements there.
Second, don't use ng-controller
on a select, you have ng-options
to dynamically place nested <option>
elements inside the <select>
.
Lastly, if you want to fill select number 2 using a value you've selected from select number 1, just reference the correct data when the ngChange callback is fired
$scope.onCountryChange = function (country) {
if(country=="gt")
{
console.log("Select GT");
$scope.serviciosgt = { ... };
}
else if(country=="sl")
{
console.log("Select SL");
$scope.serviciosgt = { ... };
}
else if(country=="hn")
{
console.log("Select HN");
$scope.serviciosgt = { ... };
}
};
your select could look something like this
<select class="form-control"
name="abvCountry"
ng-change="onCountryChange(countrySelected)"
ng-options="country for country in countries" ng-model="countrySelected">
</select>
While
$scope.countries = ["gt", "sl", "hn"];