Angular JS: $ http.get and $ routerParams

0

How to use routeParams in $ http.get so it returns only 1 object? I want to bring only the object that has the same ID passed in routeParams.

<a href="funcionario/edit/{{funcionario.id}}" class="btn btn-primary btn-xs" ng-click="">
     Editar
</button>

Clicking edit is directed to the page with the correct id. For example: localhost / official / edit / 98. I use the routes below.

var app = angular.module('funci',
                         ['ngRoute','ui.bootstrap','angular-confirm','ngSanitize' ]);

app.config(function($routeProvider, $locationProvider){

  $locationProvider.html5Mode(true);

  $routeProvider.when('/funcionarios', {
    templateUrl: 'partials/funcionarios.html',
    controller: 'FuncionariosController'
  });

  $routeProvider.when('/funcionario/novo', {
    templateUrl: 'partials/funcionario.html',
    controller: 'FuncionarioController'
  });

  $routeProvider.when('/funcionario/edit/:id', {
    templateUrl: 'partials/funcionario.html',
    controller: 'FuncionarioController'
  });

  $routeProvider.otherwise({
    redirectTo: '/funcionarios'
  });

});

On this page, I use a different controller, called: officialController. It is in this controller that I am trying to console.log only in the id object passed in routeParams, however I can not get it.

app.controller('FuncionarioController', function($scope, $http, $routeParams){
  $scope.funcionario = {};

  $http.get('http://localhost/project-funci/api/getData.php'  + $routeParams.id)
  .then(function(funcionario) {
    $scope.funcionario = funcionario.data;
    console.log($scope.funcionario);
  });

The getdata.php:

<?php
include "connect.php";

$query = "select * from funcionario";

      if ($result = mysqli_query($dbc, $query))
         {
          while($row = $result->fetch_object())
          {
              foreach($row as $key => $col){
                 $col_array[$key] = utf8_encode($col);
              }
              $row_array[] =  $col_array;
          }
         echo json_encode($row_array, JSON_NUMERIC_CHECK);
      }
      mysqli_close($dbc);

?>

Result of getData.php

  

[{"id": 98, "cod_equipment": 1, "cod_chargo": 1, "name": "

Currently, the console.log returns printed the entire html of the page.

Please, could anyone help me?

    
asked by anonymous 30.06.2017 / 05:18

1 answer

0

You are not passing the parameter in the GET URL and not getting the value passed in the php select script.

An example passing the parameter by GET would look like this:

('http://localhost/project-funci/api/getData.php?id='+ $routeParams.id)

After this, you can receive it in PHP through the global variable:

$getId = $_GET["id"];

And insert it into the sql query.

$query = "select * from funcionario WHERE id = $getId";

Remember to validate the field before sending and also validate in PHP.

    
07.07.2017 / 19:28