Dropdown Angular
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular-mocks.js"></script>
<div ng-app="myApp" ng-controller="FooController">
<label>Categorias</label>
<select ng-model="categoria" >
<option ng-repeat="x in categorias" value="{{ x.id_categoria }}">{{ x.categoria }}</option>
</select>
<label>Subcategorias</label>
<select ng-model="subcategoria">
<option value="">Selecione</option>
<option ng-repeat="x in subcategorias" value="{{ x.id_subcategoria }}">{{ x.subcategoria }}</option>
</select>
</div>
JavaScript
(function(){
var categorias = [
{
id_categoria: 1,
categoria: "Vendas"
},
{
id_categoria: 2,
categoria: "Outras receitas"
}
];
var subCategorias = [
[
{
id_categoria: 1,
id_subcategoria: 1,
subcategoria: "Internet"
},
{
id_categoria: 1,
id_subcategoria: 2,
subcategoria: "Loja"
}
],[
{
id_categoria: 2,
id_subcategoria: 3,
subcategoria: "Freela"
}
]
];
function ConfiguracaoFakes($httpBackend){
/*ignore, apenas para exemplo*/
$httpBackend.whenGET('php/carregaCategorias.php').respond(function(method, url, data, headers){
return [200, categorias, {}];
});
$httpBackend.whenGET('php/carregaSubCategorias.php?idCategoria=1').respond(function(method, url, data, headers){
return [200, subCategorias[0], {}];
});
$httpBackend.whenGET('php/carregaSubCategorias.php?idCategoria=2').respond(function(method, url, data, headers){
return [200, subCategorias[1], {}];
});
}
function FooController($http, $scope){
$http.get('php/carregaCategorias.php').then(function(response){
$scope.categorias = response.data;
});
$scope.$watch('categoria', function(newValue){
if(angular.isDefined(newValue)){
$http.get('php/carregaSubCategorias.php?idCategoria=' + newValue).then(function(response){
$scope.subcategorias = response.data;
});
}
});
}
angular.module("myApp", ['ngMockE2E'])
.run(['$httpBackend', ConfiguracaoFakes])
.controller("FooController",['$http', '$scope', FooController]);
})();