How to retrieve information from the database by id in the ionic?

6

I have an app I'm developing on ionic and angularjs I'm using routes and I'm fetching data from a database on a remote server, I have a page where I listed several categories and each category has an associated id of the category. My problem is now when I click on the category send the id to php and on the next page I list the establishments corresponding to this category but I do not know how to do it, I would like a help tips how can I do that.

Route

.state('app.estabelecimentos', {
  url: "/estabelecimentos/:id",
  views: {
  'menuContent': {
    templateUrl: "templates/estabelecimentos.html",
    controller: 'ListaDistritos'
    //controller: 'CategoriaComer'
  }
 }
})

Category listing

<div class="row" ng-repeat="cat_comer in comer">
        <div class="col">
            <a href="#/app/estabelecimentos/{{cat_comer.id}}">
                <div style="background: url(https://www.sabeonde.pt/gtm/anexos/colecoes/{{cat_comer.id_anexo}}.{{cat_comer.tipo}}); border-radius: 6px; height: 200px; background-size: 100% 100%; background-repeat: no-repeat;">
                    <div class="texto_categorias">
                        <div class="titulo_estabelecimentos">{{cat_comer.campo_slug}} ESTABELECIMENTOS</div>
                        {{cat_comer.titulo}}
                    </div>
                </div>
            </a>
        </div>
    </div>
    
asked by anonymous 28.07.2015 / 15:59

3 answers

2

Suppose you have the following controller:

angular.module('stackoverflow.example',[])
.controller('TestCtrl', function($scope, $http) {

//Rota que possui o JSON que você quer consumir.
var url = 'http://seusite.com.br/estabelecimentos/' + $scope.categoria_id

$http.get(url).
success(function(data) {
    //O parâmetro data é responsável por devolver as informações capturadas pelo servidor no seu banco de dados.
    //Você pode agora setar uma variável com estas informações (suponha que o servidor retorne um objeto Estabelecimento
    $scope.estabelecimento = data.estabelecimento;

    //Caso queira ver a resposta completa do servidor:
    console.log(data)
}).
    error(function(data, status, headers, config) {
    //É possível tratar os erros dentro desta função.
});

}

Watch out for the correct import of $ http dependency in the controller declaration in question. Whenever you are using such dependency, either in controller or services, etc. you need to import it correctly in order to use it.

    
29.09.2015 / 04:42
2

If you are passing the ID along the route, simply retrieve it as follows in your controller.

app.controller('ListaDistritos', ['$rootScope', '$http', '$stateParams', function($rootScope, $http, $stateParams) {
   $rootScope.Lista = null;

   $http.post('/RecuperarLista', {id: $routeParams.id})
   .success(function(response){
       $rootScope.Lista = response;
   });
}]);

Note that I am retrieving using $stateParams and accessing the 'id' field that was defined in your route ( :id ).

$routeParams

When using in your control, it should return an object containing all the parameters defined in state (route).

Reference .

I also recommend reading using ui-sref .

    
14.03.2016 / 21:43
1

At angular you need to create a Service and inject the competent $ http, then you call your PHP file which should return a JSON =

// Simple GET request example :
$http.get('/urldoarquivo.php').
  success(function(data, status, headers, config) {
   //aqui você recebe o json
  }).
  error(function(data, status, headers, config) {
    //Pega os erros
  });

Take a look at the link documentation

    
28.07.2015 / 16:07