How to load a value in select

2

$scope.init = function() {
$scope.modelo = {};
$scope.marca = {};
$scope.modelo = {};
$scope.modelos = [];
$scope.marcas = [];
$scope.anos = [];
$scope.precocarro=[];

$http.get('https://fipe-parallelum.rhcloud.com/api/v1/carros/marcas').success(function(data) {
  $scope.marcas = data;
});
  };
  $scope.carregarModelo = function() {
$http.get('https://fipe-parallelum.rhcloud.com/api/v1/carros/marcas/' + $scope.marca.codigo + '/modelos').success(function(data) {

  $scope.modelos = data.modelos;
})
  };

  $scope.carregarAno = function() {  
$http.get('https://fipe-parallelum.rhcloud.com/api/v1/carros/marcas/' + $scope.marca.codigo + '/modelos/' + $scope.modelo + '/anos').success(function(data) {
  $scope.anos = data;
})
  };

  $scope.consultarPreco = function(){
  var url = 'https://fipe-parallelum.rhcloud.com/api/v1/carros/marcas/' + $scope.marca.codigo + '/modelos/' + $scope.modelo + '/anos/' + $scope.anos.codigo;
  console.log(url);
  
  $http.get(url)
  .success(function (data){
      $scope.precocarro = data;
  })
  .error(function (data, status, headers, config) {
      alert('Desculpe, serviço indisponível. Tente novamente mais tarde.')
  });

}
  $scope.init();
<div class="list" ng-controller="AppCtrl">

    
    <label class="item item-input item-select">
        <span class="input-label">Marca</span>
        <select id="teste" class="customSel" ng-options="marca as marca.nome for marca in marcas" ng-model="marca" ng-change="carregarModelo()">
    </select>
    </label>

  <label class="item item-input item-select">
        <span class="input-label">Modelo</span>
         <select id="modelo" class="customSel" ng-model="modelo" ng-change="carregarAno()" >            
        <option ng-repeat="x in modelos" value="{{ x.codigo }}">{{ x.nome }}</option>
    </select>
    </label>

  <label class="item item-input item-select" ng-model="modelo" ng-change="carregarPreco()" >
        <span class="input-label">Ano</span>
         <select id="modelo" class="customSel" >
        <option value="0" selected>Selecione</option>
        <option ng-repeat="x in anos" value="{{ x.codigo }}">{{ x.nome }}</option>
    </select>
    </label>

    <button class="button button-block button-positive" ng-click="consultarPreco(modelo)">Consultar</button>
</div>
    
asked by anonymous 25.02.2017 / 21:15

2 answers

3

Your solution would look like this:

var app = angular.module("app", []);
app.controller("ctrl", ["$scope", "$http", function($scope, $http) {
  $scope.init = function() {
    $scope.modelo = {};
    $scope.marca = {};
    $scope.modelo = {};
    $scope.modelos = [];
    $scope.marcas = [];
    $scope.anos = [];
    $scope.preco = {};
    $scope.ano = {};
    $http.get('https://fipe-parallelum.rhcloud.com/api/v1/carros/marcas').success(function(data) {
      $scope.marcas = data;
    });
  };
  $scope.carregarModelo = function() {
    $http.get('https://fipe-parallelum.rhcloud.com/api/v1/carros/marcas/' + $scope.marca.codigo + '/modelos').success(function(data) {
    
      $scope.modelos = data.modelos;
    })
  };

  $scope.carregarAno = function() {  
    $http.get('https://fipe-parallelum.rhcloud.com/api/v1/carros/marcas/' + $scope.marca.codigo + '/modelos/' + $scope.modelo + '/anos').success(function(data) {
      $scope.anos = data;      
    })
  };
  $scope.consultaPreco = function()
  {
 
    $http.get('https://fipe-parallelum.rhcloud.com/api/v1/carros/marcas/' + $scope.marca.codigo + '/modelos/' + $scope.modelo + '/anos/' + $scope.ano).success(function(data){
    $scope.preco = data;
    });
  }
  $scope.init();
}]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  <label class="item item-input item-select">
            <span class="input-label">Marca</span>
            <select id="teste" class="customSel" ng-options="marca as marca.nome for marca in marcas" ng-model="marca" ng-change="carregarModelo()">
        </select>
        </label>

  <label class="item item-input item-select">
            <span class="input-label">Modelo</span>
             <select id="modelo" class="customSel" ng-model="modelo" ng-change="carregarAno()" >            
            <option ng-repeat="x in modelos" value="{{ x.codigo }}">{{ x.nome }}</option>
        </select>
        </label>

  <label class="item item-input item-select">
            <span class="input-label">Ano</span>
             <select id="ano" ng-model="ano" class="customSel" ng-change="consultaPreco()" >
            <option value="0" selected>Selecione</option>
            <option ng-repeat="x in anos" value="{{ x.codigo }}">{{ x.nome }}</option>
        </select>
        </label>
        <div>
    <p>Valor: {{preco.Valor}}</p>
    <p>Marca: {{preco.Marca}}</p>
    <p>Modelo: {{preco.Modelo}}</p>
    <p>Ano Modelo: {{preco.AnoModelo}}</p>
    <p>Combustivel: {{preco.Combustivel}}</p>
    <p>Codigo Fipe: {{preco.CodigoFipe}}</p>
    <p>Mês Referencia: {{preco.MesReferencia}}</p>
    <p>Tipo Veiculo: {{preco.TipoVeiculo}}</p>
    <p>Sigla Combustivel: {{preco.SiglaCombustivel}}</p>
  </div>
</div>

But, I would do so:

var app = angular.module("app", []);
app.controller("ctrl", ["$scope", "$http", function($scope, $http) {
  $scope.init = function() {

    $scope.modelo = {};
    $scope.marca = {};
    $scope.ano = {};
    $scope.preco = {};

    $scope.modelos = [];
    $scope.marcas = [];
    $scope.anos = [];

    $http.get('https://fipe-parallelum.rhcloud.com/api/v1/carros/marcas').success(function(data) {
      $scope.marcas = data;
    });
  };
  $scope.carregarModelo = function() {
    if ($scope.marca == null) {
      $scope.modelos = [];
      $scope.anos = [];
      $scope.preco = {};
    } else {
      $http.get('https://fipe-parallelum.rhcloud.com/api/v1/carros/marcas/' + $scope.marca.codigo + '/modelos').success(function(data) {
        $scope.modelos = data.modelos;
      });
    }
  };

  $scope.carregarAno = function() {
    if ($scope.modelo == null) {
      $scope.anos = [];
      $scope.preco = {};
    } else {
      $http.get('https://fipe-parallelum.rhcloud.com/api/v1/carros/marcas/' + $scope.marca.codigo + '/modelos/' + $scope.modelo.codigo + '/anos').success(function(data) {
        $scope.anos = data;
      });
    }
  };
  
  $scope.consultaPreco = function()
  {
    if ($scope.ano == null)
    {
    $scope.anos = [];
      $scope.preco = {};
    }
    else 
    {
    $http.get('https://fipe-parallelum.rhcloud.com/api/v1/carros/marcas/' + $scope.marca.codigo + '/modelos/' + $scope.modelo.codigo + '/anos/' + $scope.ano.codigo).success(function(data){
    $scope.preco = data;
    });
    }
  }

  $scope.init();
}]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app" ng-controller="ctrl">
  <div class="item item-input item-select">
    <span class="input-label">Marca</span>
    <select id="teste" ng-disabled="marcas.length <= 0" ng-options="ma.nome for ma in marcas" ng-model="marca" ng-change="carregarModelo()">
        <option value="">-- Escolha a Marca --</option>
    </select>
  </div>
  <br />
  <div class="item item-input item-select">
    <span class="input-label">Modelo</span>
    <select id="modelo" ng-disabled="modelos.length <= 0" ng-options="mo.nome for mo in modelos" ng-model="modelo" ng-change="carregarAno()">    
      <option value="">-- Escolha a Modelo --</option>
    </select>
  </div>
  <br />
  <div class="item item-input item-select">
    <span class="input-label">Ano</span>
    <select id="modelo" ng-disabled="anos.length <= 0" ng-change="consultaPreco()" ng-options="an.nome for an in anos" ng-model="ano">
    <option value="">-- Escolha a Ano --</option>
	</select>
  </div>
  <div>
    <p>Valor: {{preco.Valor}}</p>
    <p>Marca: {{preco.Marca}}</p>
    <p>Modelo: {{preco.Modelo}}</p>
    <p>Ano Modelo: {{preco.AnoModelo}}</p>
    <p>Combustivel: {{preco.Combustivel}}</p>
    <p>Codigo Fipe: {{preco.CodigoFipe}}</p>
    <p>Mês Referencia: {{preco.MesReferencia}}</p>
    <p>Tipo Veiculo: {{preco.TipoVeiculo}}</p>
    <p>Sigla Combustivel: {{preco.SiglaCombustivel}}</p>
  </div>
</div>
    
27.02.2017 / 15:47
2

You can make use of chained promises :

$http.get('https://fipe-parallelum.rhcloud.com/api/v1/carros/marcas')
  .then(function(data) {
      $scope.marcas = data;
      return $http.get('https://fipe-parallelum.rhcloud.com/api/v1/carros/marcas/'+marca.codigo+'/modelos');
  }).then(function (data) {
       $scope.modelo = data;
       return $http.get('https://fipe-parallelum.rhcloud.com/api/v1/carros/marcas/'+marca.codigo+'/modelos/'+ano.codigo+'/anos');
  }).then(function (data) {
       $scope.ano = data;
  });
    
25.02.2017 / 21:47