Hello thanks for help me. I have two select field, one select field for states, and other select for the cities. I am trying to load cities dinamically from the JSON generated in the server, when the user change the selected state.If you can help, many thanks..
JavaScript:
var geo = function(app){
app.controller("geoCtrl",function($scope, $http, geoFactory){
$scope.mcpios = [];
$scope.dptos = [];
$scope.$watch('dpto', function (nuevoValor, viejoValor) {
if(!viejoValor || !nuevoValor) return;
if (nuevoValor.id_departamento === viejoValor.id_departamento) { return; }
geoFactory.actMucpios($scope.dpto.id_departamento).success(function(rs){
console.log(rs[0]);
$scope.mcpios = rs[0];
});
}, true);
geoFactory.get().success( function(rs){
$scope.dptos = rs[1];
$scope.dpto = $scope.dptos[0];
});
});
app.factory("geoFactory", function($http){
var factory = {};
factory.get = function(){
return $http.get("aplicacion/controladores/controlador_geo.php?listar");
};
factory.actMucpios = function(id_dpto){
return $http.get("aplicacion/controladores/controlador_geo.php?listar&id_dpto=" + id_dpto);
}
return factory;
});
}
HTML:
<select ng-controller="geoCtrl" ng-change="actulizar()" ng-model="dpto" class="col-md-12" ng-options="dpto.nombre for dpto in dptos" >
<option ng-repeat-start>Seleccione un Departamento</option>
<option value="{{dpto.id_departamento}}" ng-repeat="dpto in dptos">{{dpto.nombre}}</option>
</select>
<select ng-model="mcpio" class="col-md-12" ng-controller="geoCtrl">
<option ng-repeat-start>Seleccione una Ciudad</option>
<option value="{{mcpio.id}}" ng-repeat="mcpio in mcpios | filter:dpto.id">{{mcpio.nombre}}</option>
</select>
I hope that you can help me... Thanks